Py File Lister
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 .
Usage :
download the source folder to your system and extract it ( in my case its in /tmp/file_lister )
then open terminal ( cmd in windows ) .
1 : python /tmp/file_lister/file_lister_tool.py
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 .
check the screen capture for more details .
Warning : This script is tested only with ubuntu 10.04 and make sure that python and pyqt is installed in your system.
[Python]
#~ ///////////////////////////////////////////////////////////////////////////////
#~ //
#~ // Copyright (c) 2010-2011, Kurian OS
#~ // All rights reserved.
#~ //
#~ // Redistribution and use in source and binary forms, with or without
#~ // modification, are permitted provided that the following conditions
#~ // are met:
#~ //
#~ // Redistributions of source code must retain the above copyright
#~ // notice, this list of conditions and the following disclaimer.
#~ // Redistributions in binary form must reproduce the above copyright
#~ // notice, this list of conditions and the following disclaimer in the
#~ // documentation and/or other materials provided with the
#~ // distribution. Neither the name of Kurian Os nor the
#~ // names of its contributors may be used to endorse or promote
#~ // products derived from this software without specific prior written
#~ // permission.
#~ //
#~ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#~ // “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#~ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
#~ // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
#~ // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
#~ // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
#~ // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#~ // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#~ // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
#~ // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#~ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
#~ // OF THE POSSIBILITY OF SUCH DAMAGE.
#~ //
#~ ///////////////////////////////////////////////////////////////////////////////
__authors__ = [“kurian.os”]
__date__ = ‘$Date: May 28, 2011 12:00:00 PM$’.split()[1]
__copyright__ = ‘2011’
__license__ = “Copyright Kurian o.S”
__contact__ = “kurianos@gmail.com”
__status__ = “Release”
from PyQt4 import QtGui, QtCore, uic
import os
import sys
res_path = ‘%s/’%(os.path.dirname(__file__))
UI_PANEL, UI_WIDGET = uic.loadUiType(“%s/file_lister.ui”%res_path)
class KCallback(object):
_callData = None
@staticmethod
def _doCall():
(func, args, kwargs) = KCallback._callData
KCallback._callData = func(*args, **kwargs)
def __init__(self,func,*args,**kwargs):
self.func = func
self.args = args
self.kwargs = kwargs
def __call__(self,*args):
KCallback._callData = (self.func, self.args, self.kwargs)
KCallback._doCall()
return KCallback._callData
class MAIN_LISTER_GUI_CLS(UI_WIDGET,UI_PANEL):
def __init__(self,*args):
UI_WIDGET.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
self.setupUi(self)
self.show()
self.log_c_button.setEnabled(False)
self.selected_log_button.setEnabled(False)
self.connect(self.source_button,QtCore.SIGNAL(‘clicked()’),KCallback(self.load_source_folder,’source’))
self.connect(self.log_button,QtCore.SIGNAL(‘clicked()’),KCallback(self.load_source_folder,’log’))
self.connect(self.scan_button,QtCore.SIGNAL(‘clicked()’),self.scan_user_folder)
self.connect(self.log_c_button,QtCore.SIGNAL(‘clicked()’),KCallback(self.create_log_file,”full”))
self.connect(self.selected_log_button,QtCore.SIGNAL(‘clicked()’),KCallback(self.create_log_file,”selected”))
def load_source_folder (self,typeCall):
options = QtGui.QFileDialog.ShowDirsOnly
folder_name = QtGui.QFileDialog.getExistingDirectory(self, ‘Open Source’,’/home’,options)
if typeCall == “source”:
self.source_path.setText(folder_name)
else:
self.log_path.setText(folder_name)
def scan_user_folder(self):
current_user_folder = str(self.source_path.text())
if current_user_folder:
if os.path.exists(current_user_folder):
for path, dirs, files in os.walk(os.path.abspath(current_user_folder)):
folder_icon = “%s/folder_pic.png”%res_path
fld_icon = QtGui.QIcon(folder_icon)
item=QtGui.QTreeWidgetItem()
item.setText(0,str(path))
item.setIcon(0,fld_icon)
self.scan_tree.addTopLevelItem(item)
for filename in files:
file_icon_path = “%s/file_pic.png”%res_path
file_icon = QtGui.QIcon(file_icon_path)
child_item = QtGui.QTreeWidgetItem()
child_item.setText(0,str(filename))
child_item.setIcon(0,file_icon)
item.addChild(child_item)
self.log_c_button.setEnabled(True)
self.selected_log_button.setEnabled(True)
else:
self.folder_not_exists(current_user_folder)
else:
self.folder_not_exists(current_user_folder)
def folder_not_exists(self,folder_path):
QtGui.QMessageBox.critical(self,”Error”,”Folder not exists %s “%folder_path, QtGui.QMessageBox.Ok)
def process_failed(self):
QtGui.QMessageBox.critical(self,”Error”,”For some unknown reason this process is failed”, QtGui.QMessageBox.Ok)
def process_complited(self,file_lister_path):
QtGui.QMessageBox.information(self,”Sucess”,”File Listing complited\nOutput Path %s”%file_lister_path, QtGui.QMessageBox.Ok)
def create_log_file(self,log_type):
log_path_root = str(self.log_path.text())
if log_path_root:
if os.path.exists(log_path_root):
log_file_tmp = “%s/file_lister_output.txt”%log_path_root
file = open(log_file_tmp,”w+”)
file.write(“#File listing is started\n”)
file.close
if log_type == “full”:
self.create_all_items_log(log_file_tmp)
else:
self.create_log_from_selected(log_file_tmp)
else:
self.folder_not_exists(log_path_root)
else:
self.folder_not_exists()
def create_all_items_log(self,log_path):
all_items_count = self.scan_tree.topLevelItemCount()
try:
for eachCnt in range(0,all_items_count):
item_list = self.scan_tree.topLevelItem(eachCnt)
folder_name = item_list.text(0)
child_count = item_list.childCount()
for eachChild in range(0,child_count):
child_item = item_list.child(eachChild)
file_name_ps = child_item.text(0)
dataLine =”%s\t\t%s\n”%(file_name_ps,folder_name)
file = open(log_path,”a+”)
file.write(dataLine)
file.close()
self.process_complited(log_path)
except:
self.process_failed()
def create_log_from_selected(self,log_path):
all_items_selected = self.scan_tree.selectedItems()
if len(all_items_selected) < 1:
self.process_failed()
return
try:
for eachItem in all_items_selected:
folder_name = eachItem.text(0)
child_count = eachItem.childCount()
for eachChild in range(0,child_count):
child_item = eachItem.child(eachChild)
file_name_ps = child_item.text(0)
dataLine ="%s\t\t%s\n"%(file_name_ps,folder_name)
file = open(log_path,"a+")
file.write(dataLine)
file.close()
self.process_complited(log_path)
except:
self.process_failed()
if __name__ == "__main__":
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
main_gui = MAIN_LISTER_GUI_CLS(None)
app.setStyle(QtGui.QStyleFactory.create('plastique'))
main_gui.show()
sys.exit(app.exec_())
[/Python]