Automating tasks in Python: Organizing files

In this new installment of the series ‘Automating tasks in Python’ I will teach you how to organize the files on your computer using the Python programming language, so that you can copies, mover, delete and compress your files automatically with few lines of code.

I will also teach you how to obtain free space on your computer using Python code to automate compression and data elimination tasks..


The module “shutil” contains the functions that allow us to copy, mover, rename and delete file in python programs.

To use these functions, you must import the corresponding module.

Copying files and folders.

To copy a file, using the form shutil, we must access the 'copy method’ which receives two values ​​as parameters (source, destination), in this way we can copy a file from one folder to another indicating the file path as the source and the final path as the destination.

In this example we go to the root folder, where the file called spam.txt is copied to the ‘delicius’ folder.

In the same way we can change the name of a file when copying it, this on the following line, donde ‘eggs.txt’ is copied to ‘delicius’ but it is renamed to ‘eggs2.txt’.

However, if we want to copy an entire folder, we also have methods to do it. To copy an entire folder, we use the 'copytree' method, which receives the same parameters, an origin and a destination, as an example:

Where the content of ‘bacon‘ it will be copied to the folder ‘bacon_backup‘, simple, true?.

Move files.

Moving files is sometimes necessary in certain programs that we carry out in python. Using the form ‘shutil‘ we can do this with few lines of code, in the same way as when we copy files.

Example:

Here we move the file ‘bacon.txt’ to the ‘eggs’ folder.

In the same way we can rename the files when moving them.

Deleting files and folders permanently

Imagine a scenario: you have a download folder, all messy, where have you downloaded from movies, songs, books to pictures. There comes a point where you need to free up space so you decide to delete the movies, but you have so many files that it is difficult to locate them in your large download folder.

Now, using python we can automate this file deletion task, let's see a little more.

We can delete files using the os module and the module shutil, with their corresponding methods:

  • os.unlink(path) To delete a file.
  • os.rmdir(path) To delete a folder that may be empty or contain other folders and files.
  • shutil.rmtree(path) To delete a folder with all its contents

Going back to the example of the movies, we can remove all the movies from your download folder in a fast way with this code:

In this code the first thing we do is import the os module, second we define a variable that contains the location of the folder that we want to analyze. Once we have the location we go through all the files that the folder path contains, and if the file name ends with a '.mp4’ that corresponds to movies, we proceed to eliminate it by passing to the ‘unlink method’ the file path.

And in this way with few lines of code we can automate the deletion of files in python.

Compress files with the zipfile module


In certain situations it is necessary to reduce the space we have. Using the download folder example again, imagine that instead of having to delete some files you could automatically compress them to free up space.

To create your own files ZIP, you must open an object of type ZipFile.
Using the method ‘write‘, you can pass the files to this object so that they can be compressed.

you must first import the module called 'zipfile’

and we create the object to pass the files to compress, with compression value as parameter.

In this way you will have created a file ‘new.zip’ that contains inside ‘spam.txt’.

To automate things more, remember the example of deleting files with an ‘.mp4’ extension, but in this case instead of deleting them you could add them to the compressed file with the write method.

Just as simple, I leave it to you as homework.

Read zip files


If we want to read the inside of a zip file to verify its content, but without having to extract your data, we can use some of the zipfile module's methods.

In this example, what is done is reading a zip file called example. Using the 'namelist method’ you can get a list of all the files it contains. We can also create a new object which can obtain information from any of the files it contains, for example spaminfo contains data about the file ‘spam.txt’ found in the compressed file. Using ‘file_size‘ Y ‘compress_size‘ we can know the weight of the original file and its weight once compressed.

Remember to always close the object.

Extract compressed files


To extract the files from a compressed file we will use the 'extractall' function, very useful in these cases.

For now this would be all, In a next tutorial I will show you how to implement everything seen in this publication to make backups automatically on your computer using Python..

Remember to follow the series of 'automating tasks in Python'.

1 thought on "Automating tasks in Python: Organizing files”

  1. Hello, could you help me and I looked for how to download large files from the web using python but I can't find it and the ones that exist are not very clear

Leave a Reply

Your email address will not be published. Required fields are marked *

five × five =