AutomateTheBoringStuff.Ch09.Projects package

Submodules

AutomateTheBoringStuff.Ch09.Projects.P1_selectiveCopy module

Selective copy

Write a function, selective_copy(), that walks through a folder tree and searches for files with a certain file extension (such as .pdf or .jpg). Copy these files from whatever location they are in to a new folder.

Note

Defaults are to check parent directory for .zip files and put them in a folder called new_folder.

AutomateTheBoringStuff.Ch09.Projects.P1_selectiveCopy.main()[source]
AutomateTheBoringStuff.Ch09.Projects.P1_selectiveCopy.selective_copy(src_folder: str = None, ext: str = None, dest_folder: str = None) → None[source]

Selective copy

Searches for given extension in given source folder (and sub folders) then copies files to given destination folder.

Parameters:
  • src_folder – String with path to source folder. Relative paths are okay.
  • ext – Extension to look for in source folder.
  • dest_folder – String with name of destination folder.
Returns:

None. Prints status messages and makes copies within destination folder.

Raises:

AttributeError – If src_folder, ext, or dest_folder are not given.

Note

Destination folder is made inside source folder. Absolute path of source folder is automatically found.

AutomateTheBoringStuff.Ch09.Projects.P2_deleteBigFiles module

P2_deleteBigFiles.py

It’s not uncommon for a few unneeded but humongous files or folders to take up the bulk of the space on your hard drive. If you’re trying to free up room on your computer, you’ll get the most bang for your buck by deleting the most massive of the unwanted files. But first you have to find them.

Write a program that walks through a folder tree and searches for exceptionally large files or folders—say, ones that have a file size of more than 100MB. (Remember, to get a file’s size, you can use os.path.getsize() from the os module.) Print these files with their absolute path to the screen.

Note

  • testfile.txt was created by typing truncate -s 101M testfile.txt in terminal.
  • Defaults to current working directory and > 100 MiB files
AutomateTheBoringStuff.Ch09.Projects.P2_deleteBigFiles.delete_big_files(folder: str = None, filesize: str = None) → None[source]

Delete big files

Checks files in given folder (and subfolders) for given filesize. If greater, file is deleted.

Parameters:
  • folder – String with folder to check files of. Relative paths are okay.
  • filesize – Maximum allowed size for files in given folder.
Returns:

None. Deletes files.

Raises:

AttributeError – If folder or filesize are not given.

Note

In debug mode - files to delete are printed to terminal. Uncomment after testing.

AutomateTheBoringStuff.Ch09.Projects.P2_deleteBigFiles.main()[source]

AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps module

P3_fillGaps.py

Write a program that finds all files with a given prefix, such as spam001.txt, spam002.txt, and so on, in a single folder and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt).

Have the program rename all the later files to close this gap.

AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.seqRegex

Regular expression object used to group numbers in filename.

Type:re.compile
AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.fill_gaps(folder: str) → None[source]

Fill gaps

Fills gaps in file name numbering of a given folder.

Parameters:folder – String containing path of folder to fill filename gaps. Relative paths are okay.
Returns:None. Prints applicable error message and renames files.

Note

Running in debug mode. Files to be renamed are printed. Uncomment after testing to rename files.

AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.get_filenames(folder: str) → list[source]

Get file names

Called by fill_gaps() to makes a list of all numbered file names in a given folder.

Breaks each file into the prefix (before numbering), numbering, and suffix (after numbering).

Parameters:folder – String containing folder path to get file names of. Relative paths are okay.
Returns:List of lists of strings with segmented names of files in alphanumerical order.
AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.get_gap(numberlist: list)[source]

Get gap

Can take an integer list returned by get_numbers() and determine the missing sequence number.

Parameters:numberlist – List of numbers to find a gap in.
Returns:Missing number in the sequence or None if there is no gap.
AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.get_numbers(files: list) → list[source]

Get numbers

Can take a list returned by get_filenames() and make an integer list of the numerical parts of the file names.

Parameters:files – List of segmented file names.
Returns:List of integers from file names in numerical order.
AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.is_sequence(numberlist: list) → bool[source]

Is sequence

Can take a list returned by get_numbers() and determine if it is a sequence based on the property list_length == (last_element - first_element + 1).

Parameters:numberlist – List containing integers to check for a sequence.
Returns:True if list contains a sequence of numbers, False otherwise.
AutomateTheBoringStuff.Ch09.Projects.P3_fillGaps.main()[source]

AutomateTheBoringStuff.Ch09.Projects.P4_makeGaps module

P4_makeGaps.py

Write a program that finds all files with a given prefix, such as spam001.txt, spam002.txt, and so on, in a single folder and locates any gaps in the numbering (such as if there is a spam001.txt and spam003.txt but no spam002.txt).

Have the program rename all the later files to close this gap.

As an added challenge, write another program that can insert gaps into numbered files so that a new file can be added.

Note

By default, uses not provided ./testdir and a gap of 2.

AutomateTheBoringStuff.Ch09.Projects.P4_makeGaps.main()[source]
AutomateTheBoringStuff.Ch09.Projects.P4_makeGaps.make_gaps(folder: str, gap: int) → None[source]

Make gaps

Makes given integer a gap in a sequence of numerical file names from a given folder.

Parameters:
  • folder – String containing path to folder to make gaps in file names. Relative paths are okay.
  • gap – Integer number to make a gap.
Returns:

None. Prints applicable error message and renames files.

Example

If folder test has files text1.txt, text2.txt, and text3.txt. Then,

>>> make_gaps('test', 2)

would change the file names to text1.txt, text3.txt, and text4.txt.

Note

Running in debug mode. Files to be renamed are printed. Uncomment after testing to rename files.

Module contents