-
Couldn't load subscription status.
- Fork 72
lesson_10 #674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
lesson_10 #674
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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/) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid hardcoded paths, at all times |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. W292 no newline at end of file |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
W292 no newline at end of file