{"id":506,"date":"2011-05-28T12:11:10","date_gmt":"2011-05-28T12:11:10","guid":{"rendered":"http:\/\/www.kurianos.com\/wordpress\/?p=506"},"modified":"2011-06-29T03:19:53","modified_gmt":"2011-06-29T03:19:53","slug":"py-file-lister","status":"publish","type":"post","link":"https:\/\/www.kurianos.com\/wordpress\/?p=506","title":{"rendered":"Py File Lister"},"content":{"rendered":"<p>A quick python tool for creating a file list from your drive or folder , ie if you have lots of file and subfolders in your drive or a main folder and you want to list all your file and file path to a file then this script will help you to do that .<\/p>\n<p>Usage :<\/p>\n<p>download the source folder to your system and extract it ( in my case its in \/tmp\/file_lister )<\/p>\n<p>then open terminal ( cmd in windows ) .<\/p>\n<p>1 : <strong>python \/tmp\/file_lister\/file_lister_tool.py <\/strong><\/p>\n<p>This will bring the main gui for you and just browse the folder you want to scan and fill the log folder where you want to create the log .<\/p>\n<p>check the screen capture for more details .<\/p>\n<p>Warning : This script is tested only with ubuntu 10.04 and make sure that python and pyqt is installed in your system.<\/p>\n<p><a href=\"http:\/\/www.kurianos.com\/python\/file_lister.tar\">Source Download<\/a><\/p>\n<p>ngg_shortcode_0_placeholder<br \/>\n[Python]<br \/>\n#~ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n#~ \/\/<br \/>\n#~ \/\/ Copyright (c) 2010-2011, Kurian OS<br \/>\n#~ \/\/ All rights reserved.<br \/>\n#~ \/\/<br \/>\n#~ \/\/ Redistribution and use in source and binary forms, with or without<br \/>\n#~ \/\/ modification, are permitted provided that the following conditions<br \/>\n#~ \/\/ are met:<br \/>\n#~ \/\/<br \/>\n#~ \/\/ Redistributions of source code must retain the above copyright<br \/>\n#~ \/\/ notice, this list of conditions and the following disclaimer.<br \/>\n#~ \/\/ Redistributions in binary form must reproduce the above copyright<br \/>\n#~ \/\/ notice, this list of conditions and the following disclaimer in the<br \/>\n#~ \/\/ documentation and\/or other materials provided with the<br \/>\n#~ \/\/ distribution.  Neither the name of Kurian Os nor the<br \/>\n#~ \/\/ names of its contributors may be used to endorse or promote<br \/>\n#~ \/\/ products derived from this software without specific prior written<br \/>\n#~ \/\/ permission.<br \/>\n#~ \/\/<br \/>\n#~ \/\/ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS<br \/>\n#~ \/\/ &#8220;AS IS&#8221; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT<br \/>\n#~ \/\/ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS<br \/>\n#~ \/\/ FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE<br \/>\n#~ \/\/ COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,<br \/>\n#~ \/\/ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES<br \/>\n#~ \/\/ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR<br \/>\n#~ \/\/ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)<br \/>\n#~ \/\/ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,<br \/>\n#~ \/\/ STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br \/>\n#~ \/\/ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED<br \/>\n#~ \/\/ OF THE POSSIBILITY OF SUCH DAMAGE.<br \/>\n#~ \/\/<br \/>\n#~ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/<br \/>\n__authors__ = [&#8220;kurian.os&#8221;]<br \/>\n__date__ = &#8216;$Date:  May 28, 2011 12:00:00 PM$&#8217;.split()[1]<br \/>\n__copyright__ = &#8216;2011&#8217;<br \/>\n__license__ = &#8220;Copyright Kurian o.S&#8221;<br \/>\n__contact__ = &#8220;kurianos@gmail.com&#8221;<br \/>\n__status__ = &#8220;Release&#8221;<\/p>\n<p>from PyQt4 import QtGui, QtCore, uic<br \/>\nimport os<br \/>\nimport sys<br \/>\nres_path = &#8216;%s\/&#8217;%(os.path.dirname(__file__))<br \/>\nUI_PANEL, UI_WIDGET = uic.loadUiType(&#8220;%s\/file_lister.ui&#8221;%res_path)<\/p>\n<p>class KCallback(object):<br \/>\n    _callData = None<br \/>\n    @staticmethod<br \/>\n    def _doCall():<br \/>\n        (func, args, kwargs) = KCallback._callData<br \/>\n        KCallback._callData = func(*args, **kwargs)<br \/>\n    def __init__(self,func,*args,**kwargs):<br \/>\n        self.func = func<br \/>\n        self.args = args<br \/>\n        self.kwargs = kwargs<br \/>\n    def __call__(self,*args):<br \/>\n        KCallback._callData = (self.func, self.args, self.kwargs)<br \/>\n        KCallback._doCall()<br \/>\n        return KCallback._callData<\/p>\n<p>class MAIN_LISTER_GUI_CLS(UI_WIDGET,UI_PANEL):<\/p>\n<p>\tdef __init__(self,*args):<br \/>\n\t\tUI_WIDGET.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)<br \/>\n\t\tself.setupUi(self)<br \/>\n\t\tself.show()<br \/>\n\t\tself.log_c_button.setEnabled(False)<br \/>\n\t\tself.selected_log_button.setEnabled(False)<br \/>\n\t\tself.connect(self.source_button,QtCore.SIGNAL(&#8216;clicked()&#8217;),KCallback(self.load_source_folder,&#8217;source&#8217;))<br \/>\n\t\tself.connect(self.log_button,QtCore.SIGNAL(&#8216;clicked()&#8217;),KCallback(self.load_source_folder,&#8217;log&#8217;))<br \/>\n\t\tself.connect(self.scan_button,QtCore.SIGNAL(&#8216;clicked()&#8217;),self.scan_user_folder)<br \/>\n\t\tself.connect(self.log_c_button,QtCore.SIGNAL(&#8216;clicked()&#8217;),KCallback(self.create_log_file,&#8221;full&#8221;))<br \/>\n\t\tself.connect(self.selected_log_button,QtCore.SIGNAL(&#8216;clicked()&#8217;),KCallback(self.create_log_file,&#8221;selected&#8221;))<\/p>\n<p>\tdef load_source_folder (self,typeCall):<br \/>\n\t\toptions = QtGui.QFileDialog.ShowDirsOnly<br \/>\n\t\tfolder_name = QtGui.QFileDialog.getExistingDirectory(self, &#8216;Open Source&#8217;,&#8217;\/home&#8217;,options)<br \/>\n\t\tif typeCall == &#8220;source&#8221;:<br \/>\n\t\t\tself.source_path.setText(folder_name)<br \/>\n\t\telse:<br \/>\n\t\t\tself.log_path.setText(folder_name)<\/p>\n<p>\tdef scan_user_folder(self):<br \/>\n\t\tcurrent_user_folder = str(self.source_path.text())<br \/>\n\t\tif current_user_folder:<br \/>\n\t\t\tif os.path.exists(current_user_folder):<br \/>\n\t\t\t\tfor path, dirs, files in os.walk(os.path.abspath(current_user_folder)):<br \/>\n\t\t\t\t\tfolder_icon = &#8220;%s\/folder_pic.png&#8221;%res_path<br \/>\n\t\t\t\t\tfld_icon = QtGui.QIcon(folder_icon)<br \/>\n\t\t\t\t\titem=QtGui.QTreeWidgetItem()<br \/>\n\t\t\t\t\titem.setText(0,str(path))<br \/>\n\t\t\t\t\titem.setIcon(0,fld_icon)<br \/>\n\t\t\t\t\tself.scan_tree.addTopLevelItem(item)<br \/>\n\t\t\t\t\tfor filename in files:<br \/>\n\t\t\t\t\t\tfile_icon_path = &#8220;%s\/file_pic.png&#8221;%res_path<br \/>\n\t\t\t\t\t\tfile_icon = QtGui.QIcon(file_icon_path)<br \/>\n\t\t\t\t\t\tchild_item = QtGui.QTreeWidgetItem()<br \/>\n\t\t\t\t\t\tchild_item.setText(0,str(filename))<br \/>\n\t\t\t\t\t\tchild_item.setIcon(0,file_icon)<br \/>\n\t\t\t\t\t\titem.addChild(child_item)<\/p>\n<p>\t\t\t\tself.log_c_button.setEnabled(True)<br \/>\n\t\t\t\tself.selected_log_button.setEnabled(True)<br \/>\n\t\t\telse:<br \/>\n\t\t\t\tself.folder_not_exists(current_user_folder)<br \/>\n\t\telse:<br \/>\n\t\t\tself.folder_not_exists(current_user_folder)<\/p>\n<p>\tdef folder_not_exists(self,folder_path):<br \/>\n\t\tQtGui.QMessageBox.critical(self,&#8221;Error&#8221;,&#8221;Folder not exists %s &#8220;%folder_path, QtGui.QMessageBox.Ok)<\/p>\n<p>\tdef process_failed(self):<br \/>\n\t\tQtGui.QMessageBox.critical(self,&#8221;Error&#8221;,&#8221;For some unknown reason this process is failed&#8221;, QtGui.QMessageBox.Ok)<\/p>\n<p>\tdef process_complited(self,file_lister_path):<br \/>\n\t\tQtGui.QMessageBox.information(self,&#8221;Sucess&#8221;,&#8221;File Listing complited\\nOutput Path %s&#8221;%file_lister_path, QtGui.QMessageBox.Ok)<\/p>\n<p>\tdef create_log_file(self,log_type):<br \/>\n\t\tlog_path_root = str(self.log_path.text())<br \/>\n\t\tif log_path_root:<br \/>\n\t\t\tif os.path.exists(log_path_root):<br \/>\n\t\t\t\tlog_file_tmp = &#8220;%s\/file_lister_output.txt&#8221;%log_path_root<br \/>\n\t\t\t\tfile = open(log_file_tmp,&#8221;w+&#8221;)<br \/>\n\t\t\t\tfile.write(&#8220;#File listing is started\\n&#8221;)<br \/>\n\t\t\t\tfile.close<br \/>\n\t\t\t\tif log_type == &#8220;full&#8221;:<br \/>\n\t\t\t\t\tself.create_all_items_log(log_file_tmp)<br \/>\n\t\t\t\telse:<br \/>\n\t\t\t\t\tself.create_log_from_selected(log_file_tmp)<br \/>\n\t\t\telse:<br \/>\n\t\t\t\tself.folder_not_exists(log_path_root)<br \/>\n\t\telse:<br \/>\n\t\t\tself.folder_not_exists()<\/p>\n<p>\tdef create_all_items_log(self,log_path):<br \/>\n\t\tall_items_count = self.scan_tree.topLevelItemCount()<br \/>\n\t\ttry:<br \/>\n\t\t\tfor eachCnt in range(0,all_items_count):<br \/>\n\t\t\t\titem_list = self.scan_tree.topLevelItem(eachCnt)<br \/>\n\t\t\t\tfolder_name = item_list.text(0)<br \/>\n\t\t\t\tchild_count = item_list.childCount()<br \/>\n\t\t\t\tfor eachChild in range(0,child_count):<br \/>\n\t\t\t\t\tchild_item = item_list.child(eachChild)<br \/>\n\t\t\t\t\tfile_name_ps = child_item.text(0)<br \/>\n\t\t\t\t\tdataLine =&#8221;%s\\t\\t%s\\n&#8221;%(file_name_ps,folder_name)<br \/>\n\t\t\t\t\tfile = open(log_path,&#8221;a+&#8221;)<br \/>\n\t\t\t\t\tfile.write(dataLine)<br \/>\n\t\t\t\t\tfile.close()<br \/>\n\t\t\tself.process_complited(log_path)<br \/>\n\t\texcept:<br \/>\n\t\t\tself.process_failed()<\/p>\n<p>\tdef create_log_from_selected(self,log_path):<br \/>\n\t\tall_items_selected = self.scan_tree.selectedItems()<br \/>\n\t\tif len(all_items_selected) < 1:\n\t\t\tself.process_failed()\n\t\t\treturn\n\t\ttry:\n\t\t\tfor eachItem in all_items_selected:\n\t\t\t\tfolder_name = eachItem.text(0)\n\t\t\t\tchild_count = eachItem.childCount()\n\t\t\t\tfor eachChild in range(0,child_count):\n\t\t\t\t\tchild_item = eachItem.child(eachChild)\n\t\t\t\t\tfile_name_ps = child_item.text(0)\n\t\t\t\t\tdataLine =\"%s\\t\\t%s\\n\"%(file_name_ps,folder_name)\n\t\t\t\t\tfile = open(log_path,\"a+\")\n\t\t\t\t\tfile.write(dataLine)\n\t\t\t\t\tfile.close()\n\t\t\tself.process_complited(log_path)\n\t\texcept:\n\t\t\tself.process_failed()\n\nif __name__ == \"__main__\":\n\tfrom PyQt4 import QtGui, QtCore\n\tapp = QtGui.QApplication(sys.argv)\n\tmain_gui = MAIN_LISTER_GUI_CLS(None)\n\tapp.setStyle(QtGui.QStyleFactory.create('plastique'))\n\tmain_gui.show()\n\tsys.exit(app.exec_())\n\n[\/Python]\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A quick python tool for creating a file list from your drive or folder , ie if you have lots of file and subfolders in your drive or a main folder and you want to list all your file and file path to a file then this script will help you to do that . [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[15,14],"tags":[],"class_list":["post-506","post","type-post","status-publish","format-standard","hentry","category-linux","category-python"],"_links":{"self":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/506","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=506"}],"version-history":[{"count":8,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/506\/revisions"}],"predecessor-version":[{"id":529,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/506\/revisions\/529"}],"wp:attachment":[{"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=506"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=506"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kurianos.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=506"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}