Working With Files In Python
@marvinMarvin I write code and stay weird. An eternal child. Most of us have had that one experience where we had a ton of dis-organized files in our machines. It happens. One minute, you’re opening a large zip file, the next thing you know, the files are everywhere in the directory, mixing with all your important files and randomly placed leaving you with the task of manually sorting what needs to go where. It’s a real pain. To ease this process, we’re going to delve into file management with python the smart way. Work smart, not hard. Let’s begin. We’ll be using python 3.4 or greater. Assuming you’ve got python up and running already, we’re going to take a walk with the OS module and a few others we will introduce along the way. Most of these come with python, so there’s no need to install anything else to follow along. Creating random files Create a directory to work with. Call it ManageFiles . Inside this folder create another folder RandomFiles . Your directory structure should now look like this: ManageFiles/ | |_RandomFiles/ We’re going to create random files to play with in the RandomFiles directory Create a file create_random_files.py inside ManageFiles directory. You now have this: ManageFiles/ | |_ create_random_files.py |_RandomFiles/ Done? Now get in the following code, we’ll get into its details in a moment. import os from pathlib import Path import random list_of_extensions = [‘.rst’,’.txt’,’.md’,’.docx’,’.odt’,’.html’,’.ppt’,’.doc’] # get into the RandomFiles directory os.chdir(‘./RandomFiles’) for item in list_of_extensions: # create 20 random files for each file extension for num in range(20): # let the file begin with a random number between 1 to 50 file_name = random.randint(1,50) file_to_create = str(file_name) item Path(file_to_create).touch() As of python 3.4, we ‘ve got pathlib , our little magic box. We also import python’s random function for creating random numbers; Hold on to that thought, we’re going to cover it when as we get to the line that uses it. First off, we create a list of file extensions from where we will get our random files. Feel free to add to it. Next up, we change to the RandomFiles directory, then comes our loop, so here goes. We are simply saying, take each item in this list_of_extensions and do the following to it. Let’s take the .txt for instance. We get into another loop, where to this .txt , we do something to it 20 times. Remember our import of random ? We use it to select a random number between 1 and 50 for our file. In short, what this little loop does is save us, the less creative lot(don’t worry, I’m part of this crew), the time of naming random files. We will simply create a file say 23.txt or 14.txt , provided it falls within our range of 50, twenty times. This is just so as to create a mess large enough to give pain when moving manually. The same process will be done with the other extensions. Next? Run this in your terminal. python create_random_files.py Congratulations! We now have a mess of a directory. Now to clean it up. In the same location where our create_random_files.py is, » Read More
Like to keep reading?
This article first appeared on hackernoon.com. If you'd like to keep reading, follow the white rabbit.