Manipulating files and directories

Let's see a couple of quick examples on how to manipulate files and directories. The first example manipulates the content:

# files/manipulation.py
from collections import Counter
from string import ascii_letters

chars = ascii_letters + ' '

def sanitize(s, chars):
return ''.join(c for c in s if c in chars)

def reverse(s):
return s[::-1]

with open('fear.txt') as stream:
lines = [line.rstrip() for line in stream]

with open('raef.txt', 'w') as stream:
stream.write(' '.join(reverse(line) for line in lines))

# now we can calculate some statistics
lines = [sanitize(line, chars) for line in lines]
whole = ' '.join(lines)
cnt = Counter(whole.lower().split())
print(cnt.most_common(3))

The previous example defines two functions: sanitize and reverse. They are simple functions whose purpose is to remove anything that is not a letter or space from a string, and produce the reversed copy of a string, respectively.

We open fear.txt and we read its content into a list. Then we create a new file, raef.txt, which will contain the horizontally-mirrored version of the original one. We write all the content of lines with a single operation, using join on a new line character. Maybe more interesting, is the bit in the end. First, we reassign lines to a sanitized version of itself, by means of list comprehension. Then we put them together in the whole string, and finally, we pass the result to Counter. Notice that we split the string and put it in lowercase. This way, each word will be counted correctly, regardless of its case, and, thanks to split, we don't need to worry about extra spaces anywhere. When we print the three most common words, we realize that truly Thich Nhat Hanh's focus is on others, as we is the most common word in the text:

$ python manipulation.py
[('we', 17), ('the', 13), ('were', 7)]

Let's now see an example of manipulation more oriented to disk operations, in which we put the shutil module to use:

# files/ops_create.py
import shutil
import os

BASE_PATH = 'ops_example' # this will be our base path
os.mkdir(BASE_PATH)

path_b = os.path.join(BASE_PATH, 'A', 'B')
path_c = os.path.join(BASE_PATH, 'A', 'C')
path_d = os.path.join(BASE_PATH, 'A', 'D')

os.makedirs(path_b)
os.makedirs(path_c)

for filename in ('ex1.txt', 'ex2.txt', 'ex3.txt'):
with open(os.path.join(path_b, filename), 'w') as stream:
stream.write(f'Some content here in {filename} ')

shutil.move(path_b, path_d)

shutil.move(
os.path.join(path_d, 'ex1.txt'),
os.path.join(path_d, 'ex1d.txt')
)

In the previous code, we start by declaring a base path, which will safely contain all the files and folders we're going to create. We then use makedirs to create two directories: ops_example/A/B and ops_example/A/C. (Can you think of a way of creating the two directories by using map?).

We use os.path.join to concatenate directory names, as using / would specialize the code to run on a platform where the directory separator is /, but then the code would fail on platforms with a different separator. Let's delegate to join the task to figure out which is the appropriate separator.

After creating the directories, within a simple for loop, we put some code that creates three files in directory B. Then, we move the folder B and its content to a different name: D. And finally, we rename ex1.txt to ex1d.txt. If you open that file, you'll see it still contains the original text from the for loop. Calling tree on the result produces the following:

$ tree ops_example/
ops_example/
└── A
├── C
└── D
├── ex1d.txt
├── ex2.txt
└── ex3.txt
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20