Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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")
Copy link
Contributor

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

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)
Copy link
Contributor

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