diff --git a/students/dychowicz_monika/lesson_10_organizing_files/README.md b/students/dychowicz_monika/lesson_10_organizing_files/README.md new file mode 100644 index 000000000..6635e3f98 --- /dev/null +++ b/students/dychowicz_monika/lesson_10_organizing_files/README.md @@ -0,0 +1,7 @@ +### Lesson 10 - Organizing Files +#### introduction +- [Automate the Boring Stuff with Python / Chapter 9](https://automatetheboringstuff.com/chapter9/) +#### practice projects +1. [Automate the Boring Stuff with Python / Chapter 9 / Selective Copy](https://automatetheboringstuff.com/chapter9/) +1. [Automate the Boring Stuff with Python / Chapter 9 / Deleting Unneeded Files](https://automatetheboringstuff.com/chapter9/) +1. [Automate the Boring Stuff with Python / Chapter 9 / Filling in the Gaps](https://automatetheboringstuff.com/chapter9/) diff --git a/students/dychowicz_monika/lesson_10_organizing_files/deleting_unneeded_files.py b/students/dychowicz_monika/lesson_10_organizing_files/deleting_unneeded_files.py new file mode 100644 index 000000000..04202aa21 --- /dev/null +++ b/students/dychowicz_monika/lesson_10_organizing_files/deleting_unneeded_files.py @@ -0,0 +1,15 @@ +import os + +max_size = 100000000 + + +def delete_unneeded_files(folder): + for foldername, subfolders, filenames in os.walk(folder): + for filename in filenames: + filename = os.path.join(folder, filename) + if os.path.getsize(filename) > max_size: + print(filename) + + +if __name__ == '__main__': + delete_unneeded_files(r"C:\Users\dychomon\Downloads") \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_10_organizing_files/filling_the_gaps.properties.py b/students/dychowicz_monika/lesson_10_organizing_files/filling_the_gaps.properties.py new file mode 100644 index 000000000..427eb459a --- /dev/null +++ b/students/dychowicz_monika/lesson_10_organizing_files/filling_the_gaps.properties.py @@ -0,0 +1,30 @@ +import os +import re + +prefix = "spam" +pattern = re.compile(prefix + r'.*(\d{3})') + + +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__': + fill_the_gaps("C:\Python36\gaps") \ No newline at end of file diff --git a/students/dychowicz_monika/lesson_10_organizing_files/selective_copy.py b/students/dychowicz_monika/lesson_10_organizing_files/selective_copy.py new file mode 100644 index 000000000..a3d46ed82 --- /dev/null +++ b/students/dychowicz_monika/lesson_10_organizing_files/selective_copy.py @@ -0,0 +1,18 @@ +import os +import shutil + + +def selective_copy(source_folder, destination_folder, extension): + for foldername, subfolders, filenames in os.walk(source_folder): + for filename in filenames: + if filename.lower().endswith(extension): + shutil.copy(os.path.join(source_folder, filename), + destination_folder) + + +source = r'C:\python_to_copy' +destination = r'C:\python_copied' +ext = '.txt' + +if __name__ == '__main__': + selective_copy(source, destination, ext) \ No newline at end of file