From e19d8669a6456f595488b0cd97e36593faca23d7 Mon Sep 17 00:00:00 2001 From: kana800 Date: Mon, 20 Jul 2020 04:32:46 -0400 Subject: [PATCH 1/2] add-alphabetical-file --- .../README.md | 9 +++ .../main.py | 70 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 projects/Write script to move files into alphabetically ordered folder/README.md create mode 100644 projects/Write script to move files into alphabetically ordered folder/main.py diff --git a/projects/Write script to move files into alphabetically ordered folder/README.md b/projects/Write script to move files into alphabetically ordered folder/README.md new file mode 100644 index 00000000..05ad56a7 --- /dev/null +++ b/projects/Write script to move files into alphabetically ordered folder/README.md @@ -0,0 +1,9 @@ +## Script to organize files in a directory (alphabetical order) + +### usage + +```python +python main.py +``` + +Folder will be generated and files will be moved accordingly. diff --git a/projects/Write script to move files into alphabetically ordered folder/main.py b/projects/Write script to move files into alphabetically ordered folder/main.py new file mode 100644 index 00000000..c08e5121 --- /dev/null +++ b/projects/Write script to move files into alphabetically ordered folder/main.py @@ -0,0 +1,70 @@ +''' +This script will sort and move the files in the directory (the alphabetical order). + +'apple.txt' --> 'A' +'ryan.txt' --> 'R' +'01010.txt' --> 'Misc' + +''' +import os +import shutil + +filenames = [] + +def getfoldername(filename): + ''' + 'Test.txt' --> 't' + '010.txt' --> 'misc' + 'zebra.txt' --> 'z' + 'Alpha@@.txt' --> 'a' + '!@#.txt' --> 'misc' + ''' + if filename[0].isalpha(): + return filename[0].lower() + else: + return 'misc' + +#read the names of the files +def readdirectory(): + ''' + read the filename in the current directory and append them to a list + ''' + global filenames + for files in os.listdir(os.getcwd()): + if os.path.isfile(os.path.join(os.getcwd(),files)): + filenames.append(files) + filenames.remove('main.py') #removing the filename of the script + +#getting the first letters of the file & creating a file in the current_dir +def createfolder(): + ''' + creating a folders + ''' + global filenames + for f in filenames: + if os.path.isdir(getfoldername(f)): + print("folder already created") + else: + os.mkdir(getfoldername(f)) + print(f'creating folder...') + +#moving the file into the proper folder +def movetofolder(): + ''' + movetofolder('zebra.py','z') + + 'zebra.py'(moved to) 'z' + ''' + global filenames + for i in filenames: + filename = i + file = getfoldername(i) + source = os.path.join(os.getcwd(),filename) + destination = os.path.join(os.getcwd(),file) + print(f"moving {source} to {destination}") + shutil.move(source,destination) + +if __name__ == '__main__': + readdirectory() + createfolder() + movetofolder() From 3dbbd1046ca3210a47ec20ae5e7ba0a8ed2995eb Mon Sep 17 00:00:00 2001 From: kana800 Date: Mon, 20 Jul 2020 09:54:31 -0400 Subject: [PATCH 2/2] addalphabetical-file --- .../main.py | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/projects/Write script to move files into alphabetically ordered folder/main.py b/projects/Write script to move files into alphabetically ordered folder/main.py index c08e5121..5af8c5cc 100644 --- a/projects/Write script to move files into alphabetically ordered folder/main.py +++ b/projects/Write script to move files into alphabetically ordered folder/main.py @@ -1,5 +1,6 @@ ''' -This script will sort and move the files in the directory (the alphabetical order). +This script will sort and move the files in the directory +(the alphabetical order). 'apple.txt' --> 'A' 'ryan.txt' --> 'R' @@ -11,6 +12,7 @@ filenames = [] + def getfoldername(filename): ''' 'Test.txt' --> 't' @@ -24,18 +26,19 @@ def getfoldername(filename): else: return 'misc' -#read the names of the files + def readdirectory(): ''' read the filename in the current directory and append them to a list ''' global filenames for files in os.listdir(os.getcwd()): - if os.path.isfile(os.path.join(os.getcwd(),files)): + if os.path.isfile(os.path.join(os.getcwd(), files)): filenames.append(files) - filenames.remove('main.py') #removing the filename of the script + filenames.remove('main.py') # removing script from the file list + -#getting the first letters of the file & creating a file in the current_dir +# getting the first letters of the file & creating a file in the current_dir def createfolder(): ''' creating a folders @@ -46,9 +49,10 @@ def createfolder(): print("folder already created") else: os.mkdir(getfoldername(f)) - print(f'creating folder...') + print('creating folder...') -#moving the file into the proper folder + +# moving the file into the proper folder def movetofolder(): ''' movetofolder('zebra.py','z') @@ -59,10 +63,11 @@ def movetofolder(): for i in filenames: filename = i file = getfoldername(i) - source = os.path.join(os.getcwd(),filename) - destination = os.path.join(os.getcwd(),file) + source = os.path.join(os.getcwd(), filename) + destination = os.path.join(os.getcwd(), file) print(f"moving {source} to {destination}") - shutil.move(source,destination) + shutil.move(source, destination) + if __name__ == '__main__': readdirectory()