From 996d0a4778f323c0006cb16ce056f4f3a056861a Mon Sep 17 00:00:00 2001 From: VladKappa Date: Tue, 23 Jun 2020 09:40:51 +0300 Subject: [PATCH 1/2] The code now follows pep8 guidelines --- .../Random_word_from_list.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/projects/Random_word_from_list/Random_word_from_list.py b/projects/Random_word_from_list/Random_word_from_list.py index f47bdf23..e6bf476d 100644 --- a/projects/Random_word_from_list/Random_word_from_list.py +++ b/projects/Random_word_from_list/Random_word_from_list.py @@ -1,24 +1,29 @@ import sys import random +# check if filename is supplied as a command line argument if sys.argv[1:]: - filename = sys.argv[1] # check if filename is supplied as a command line argument + filename = sys.argv[1] else: filename = input("What is the name of the file? (extension included): ") try: file = open(filename) -except: - print("File doesn't exist!") # handle exception +except (FileNotFoundError, IOError): + print("File doesn't exist!") exit() +# handle exception -num_lines = sum(1 for line in file if line.rstrip()) # get number of lines +# get number of lines +num_lines = sum(1 for line in file if line.rstrip()) -random_line = random.randint(0, num_lines) # generate a random number between possible interval +# generate a random number between possible interval +random_line = random.randint(0, num_lines) -file.seek(0) # re-iterate from first line +# re-iterate from first line +file.seek(0) for i, line in enumerate(file): if i == random_line: - print(line.rstrip()) # rstrip removes any trailing newlines :) + print(line.rstrip()) # rstrip removes any trailing newlines :) break From c51044488b26e9152afbe674d00ad23d8ff8e60e Mon Sep 17 00:00:00 2001 From: Mitesh <46081187+Mitesh2499@users.noreply.github.com> Date: Tue, 23 Jun 2020 21:13:57 +0530 Subject: [PATCH 2/2] Add files via upload --- .../README.md | 2 + .../file-sortor.py | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 projects/organized download folder with different categories/README.md create mode 100644 projects/organized download folder with different categories/file-sortor.py diff --git a/projects/organized download folder with different categories/README.md b/projects/organized download folder with different categories/README.md new file mode 100644 index 00000000..44eb9440 --- /dev/null +++ b/projects/organized download folder with different categories/README.md @@ -0,0 +1,2 @@ +# Files-Sorter +This is python script that sort the files in Download directory to other directories depending on extention. diff --git a/projects/organized download folder with different categories/file-sortor.py b/projects/organized download folder with different categories/file-sortor.py new file mode 100644 index 00000000..75b93c4d --- /dev/null +++ b/projects/organized download folder with different categories/file-sortor.py @@ -0,0 +1,46 @@ +import os +import shutil + +os.chdir("E:\\downloads") +# print(os.getcwd()) + +# check number of files in directory +files = os.listdir() + +# list of extension (You can add more if you want) +extentions = { + "images": [".jpg", ".png", ".jpeg", ".gif"], + "videos": [".mp4", ".mkv"], + "musics": [".mp3", ".wav"], + "zip": [".zip", ".tgz", ".rar", ".tar"], + "documents": [".pdf", ".docx", ".csv", + ".xlsx", ".pptx", ".doc", ".ppt", ".xls"], + "setup": [".msi", ".exe"], + "programs": [".py", ".c", ".cpp", ".php", ".C", ".CPP"], + "design": [".xd", ".psd"], +} + + +# sort to specific folder depend on extenstions +def sorting(file): + keys = list(extentions.keys()) + for key in keys: + for ext in extentions[key]: + # print(ext) + if file.endswith(ext): + return key + + +# iterate through each file +for file in files: + dist = sorting(file) + if dist: + try: + shutil.move(file, "../download-sorting/" + dist) + except Exception: + print(file + " is already exist") + else: + try: + shutil.move(file, "../download-sorting/others") + except Exception: + print(file + " is already exist")