Skip to content

Instantly share code, notes, and snippets.

@biwin
Created September 14, 2014 18:16
Show Gist options
  • Save biwin/6762b99158480dd3e6bf to your computer and use it in GitHub Desktop.
Save biwin/6762b99158480dd3e6bf to your computer and use it in GitHub Desktop.
A simple python3 program to write the list of files and folders in a given directory to a tesxt file.
__author__ = 'payload'
from os import listdir
# this program copies the file/folder names in a given directory to a text file.
# start the program
proceed = True
while proceed is True:
file_list = []
file_name_first = input("Enter the output file name")
file_name = file_name_first + '.txt'
# print("Started to write in the " + file_name)
file = open(file_name, 'w')
write_cycle = True
while write_cycle is True:
dir_okay = False
# gets the directory
while dir_okay is False:
print("Enter the path to be recorded:")
path = input("eg. /home/user/ or 'q' to quit entering:")
if path == 'q':
write_cycle = False
break
else:
# opens the directory
try:
file_list = listdir(path)
dir_okay = True
# process the directory
file_list.sort()
disk_no = input("Enter the disk number!")
directory_name = input("Enter the directory/disk name:")
# print('wrote the file ' + directory_name + str(file_list))
file.write(disk_no + "\t")
file.write(directory_name + "\t")
for directory in file_list:
file.write(" \t\t\t" + directory + "\n")
except FileExistsError:
print('Error: The file already exist!')
dir_okay = False
except FileNotFoundError:
print('Error: File Not Found!')
dir_okay = False
except InterruptedError:
print('Error: Operation Interrupted!!')
dir_okay = False
except PermissionError:
print('Error: Permission denied!')
dir_okay = False
except TimeoutError:
print('Error: Operation timed out!')
dir_okay = False
file.close()
print('The task is completed! do you want to start another task?')
confirm = input("Enter 'no' to quit, 'yes' to start a new tas!")
# exit the program
if confirm == 'yes':
print('Now starting a new task!')
proceed = True
continue
else:
print('Re enter the files now!!')
break
print("Quitting the program . . . ")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment