diff --git a/students/dychowicz_monika/lesson_11_argparse_and_math/README.md b/students/dychowicz_monika/lesson_11_argparse_and_math/README.md new file mode 100644 index 000000000..a227ad869 --- /dev/null +++ b/students/dychowicz_monika/lesson_11_argparse_and_math/README.md @@ -0,0 +1,11 @@ +### Lesson 11 - argparse + math +#### introduction +- [What is argparse?](http://www.bogotobogo.com/python/python_argparse.php) +- [argparse — Command-Line Option and Argument Parsing](https://pymotw.com/3/argparse/) +#### practice projects +1. Modify project 1 from lesson 9 to use argparse for parsing input +1. Modify project 2 from lesson 9 to use argparse for parsing input +1. Modify project 3 from lesson 9 to use argparse for parsing input +1. Modify project 1 from lesson 10 to use argparse for parsing input +1. Modify project 2 from lesson 10 to use argparse for parsing input +1. Modify project 3 from lesson 10 to use argparse for parsing input diff --git a/students/dychowicz_monika/lesson_11_argparse_and_math/filling_the_gaps_argparse.py b/students/dychowicz_monika/lesson_11_argparse_and_math/filling_the_gaps_argparse.py new file mode 100644 index 000000000..ae4f0d622 --- /dev/null +++ b/students/dychowicz_monika/lesson_11_argparse_and_math/filling_the_gaps_argparse.py @@ -0,0 +1,42 @@ +import os +import re +import argparse + + +def check_args(args=None): + parser = argparse.ArgumentParser() + parser.add_argument("-p", "--prefix", + help="prefix", + default="spam") + parser.add_argument("-d", "--directory", + help="directory", + default=r"C:\Python36\gaps") + results = parser.parse_args(args) + return results.prefix, results.directory + + +def fill_the_gaps(folder): + for foldername, subfolders, filenames in os.walk(folder): + filenames = sorted(filenames) + index = 1 + new_filename = "" + for filename in filenames: + if pattern.match(filename): + if index == 1: + new_filename = filename + print(filename, new_filename, index) + if filename != new_filename: + print( + "Lack of filename: " + new_filename + "\nFile " + + filename + " will be renamed.") + filename_path = os.path.join(foldername, filename) + new_filename_path = os.path.join(foldername, new_filename) + os.rename(filename_path, new_filename_path) + new_filename = filename.replace(str(index), str(index + 1)) + index += 1 + + +if __name__ == '__main__': + prefix, directory = check_args() + pattern = re.compile(prefix + r'.*(\d{3})') + fill_the_gaps(directory) \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_11_argparse_and_math/regex_search_argparse.py b/students/dychowicz_monika/lesson_11_argparse_and_math/regex_search_argparse.py new file mode 100644 index 000000000..52ac65c79 --- /dev/null +++ b/students/dychowicz_monika/lesson_11_argparse_and_math/regex_search_argparse.py @@ -0,0 +1,31 @@ +import os +import re +import argparse + + +def check_args(args=None): + parser = argparse.ArgumentParser() + parser.add_argument("-r", "--regex", + help="regex", + default=".*") + parser.add_argument("-d", "--directory", + help="directory", + default=r"C:\Python36\files") + results = parser.parse_args(args) + return results.regex, results.directory + + +def regex_search(reg, folder): + regex_text = re.compile(reg) + files_list = os.listdir(folder) + for file in files_list: + if file.endswith('.txt'): + with open(os.path.join(folder, file)) as textfile: + for line in textfile: + if regex_text.match(line): + print("Regex found " + line) + + +if __name__ == '__main__': + regex, directory = check_args() + regex_search(regex, directory) \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_11_argparse_and_math/selective_copy_argparse.py b/students/dychowicz_monika/lesson_11_argparse_and_math/selective_copy_argparse.py new file mode 100644 index 000000000..64596e188 --- /dev/null +++ b/students/dychowicz_monika/lesson_11_argparse_and_math/selective_copy_argparse.py @@ -0,0 +1,31 @@ +import os +import shutil +import argparse + + +def check_args(args=None): + parser = argparse.ArgumentParser() + parser.add_argument("-s", "--source", + help="source directory", + default=r'C:\python_to_copy') + parser.add_argument("-d", "--destination", + help="destination directory", + default=r'C:\python_copied') + parser.add_argument("-e", "--extension", + help="extension", + default=".txt") + results = parser.parse_args(args) + return results.source, results.destination, results.extension + + +def selective_copy(source_folder, destination_folder, ext): + for foldername, subfolders, filenames in os.walk(source_folder): + for filename in filenames: + if filename.lower().endswith(ext): + shutil.copy(os.path.join(source_folder, filename), + destination_folder) + + +if __name__ == '__main__': + source, destination, extension = check_args() + selective_copy(source, destination, extension) \ No newline at end of file