<a href="https://automatetheboringstuff.com/chapter8/" target = "_blank">Files</a>

Files have a name and a path. The root folder is the lowest folder. It's c:\ on Windows and / on Linux and Mac.

In a file path, the folders and filenames are separated by backslash \ on Windows and forward slash / on Mac & Linux

In [1]:
print('c:\\spam\\eggs.png')
c:\spam\eggs.png
In [2]:
print(r'c:\spam\eggs.png')
c:\spam\eggs.png

You can use the join() string method but it's for Windows only. An better alternative is to use the os module

In [3]:
'\\'.join(['folder1', 'folder2', 'folder3', 'file.png'])
Out[3]:
'folder1\\folder2\\folder3\\file.png'

os.path.join()

Use the os.path.join() to combine folders with the correct slash

In [4]:
import os
os.path.join('folder1', 'folder2', 'folder3', 'file.png')
Out[4]:
'folder1\\folder2\\folder3\\file.png'
In [5]:
os.sep
Out[5]:
'\\'

os.getcwd()

os.getcwd() will return the current work directory. The current working directory is the folder that any relative paths are relative to.

In [6]:
os.getcwd()
Out[6]:
'z:\\it\\Python\\General Python\\11-Files'

os.chdir()

os.chdir() will change the directory

In [7]:
os.chdir('c:\\')
os.getcwd()
Out[7]:
'c:\\'

Absolute and Relative Paths

Absolute paths begin with the root folder e.g. 'c:\\folder1\\folder2\\spam.png'. Relative paths do not begins with the root folder e.g. 'folder1\\folder2\\spam.png'

The . and .. Folders

The . folder represents this (current) folder. The .. folder represents the parent folder

img

os.path.abspath() os.path.isabs()

os.path.abspath() returns an absolute path from the path passed to it

In [8]:
os.path.abspath('spam.png')
Out[8]:
'c:\\spam.png'
In [9]:
os.path.abspath('..\\..\\spam.png')
Out[9]:
'c:\\spam.png'

os.path.isabs() returns True if the path passed to it is absolute

In [10]:
os.path.isabs('..\\..\\spam.png')
Out[10]:
False
In [11]:
os.path.isabs('c:\\folder\\spam.png')
Out[11]:
True

os.path.relpath()

os.path.relpath() return the relative path between two paths passed to it

In [12]:
os.path.relpath('c:\\folder1\\folder2\\spam.png', 'c:\\folder1')
Out[12]:
'folder2\\spam.png'

os.path.dirname() os.path.basename()

In [13]:
os.path.dirname('c:\\folder1\\folder2\\spam.png')
Out[13]:
'c:\\folder1\\folder2'
In [14]:
os.path.basename('c:\\folder1\\folder2\\spam.png')
Out[14]:
'spam.png'
In [15]:
os.path.basename('c:\\folder1\\folder2')
Out[15]:
'folder2'

os.path.exists()

os.path.exists() returns True if the filename passed to it exists

In [16]:
os.path.exists('c:\\folder1\\folder2\\spam.png')
Out[16]:
False
In [17]:
os.path.exists('c:\\windows')
Out[17]:
True

os.path.isfile() os.path.isdir()

In [18]:
os.path.isfile('c:\\windows\\system32\\calc.exe')
Out[18]:
True
In [19]:
os.path.isfile('c:\\windows\\system32')
Out[19]:
False
In [20]:
os.path.isdir('c:\\windows\\system32\\calc.exe')
Out[20]:
False
In [21]:
os.path.isdir('c:\\windows\\system32')
Out[21]:
True

os.path.getsize() os.listdir()

os.path.getsize() returns a file's size

In [22]:
os.path.getsize('c:\\windows\\system32\\calc.exe')
Out[22]:
26112

os.listdir() returns a list of strings of filenames

In [23]:
os.listdir('c:\\Users\\Public\\Music')
Out[23]:
['desktop.ini', 'Sample Music']

Example Code: Finding the total size of all files in a folder

In [24]:
totalSize = 0
for filename in os.listdir('z:\\it'):
    if not os.path.isfile(os.path.join('z:\\it', filename)):
        continue
    totalSize = totalSize + os.path.getsize(os.path.join('z:\\it', filename))
totalSize
Out[24]:
9

os.makedirs()

os.makedirs() can make folders

In [25]:
# os.makedirs('z:\\temp')

Reading and Writing Plaintext Files

The open() Function

open() will return a file object which has reading and writing-related methods.

Read/ Write/ Append Mode

Passing r (or nothing) to open() will open the file in read mode, w for write mode, a for append mode. Opening a non-exist filename in write or append mode will create that file.

In [26]:
helloFile = open('Z:\\IT\\Python\\General Python\\Snippets\\hello.txt')
helloFile.close()
In [27]:
helloFile = open('Z:\\IT\\Python\\General Python\\Snippets\\hello2.txt', 'w')
helloFile.close()
In [28]:
helloFile = open('Z:\\IT\\Python\\General Python\\Snippets\\hello.txt', 'a')

The read() Method

Call read() to read the contents of a file

In [29]:
# helloFile.read()
In [30]:
# content = helloFile.read()
# print(content)

helloFile.close()

The readlines() Method

Call readlines() to return a list of strings of the file's content

In [31]:
helloFile = open('Z:\\IT\\Python\\General Python\\Snippets\\hello.txt')
helloFile.readlines()
Out[31]:
['Hello world!\n', 'How are you?']

The write() Method

Call write() to write contents (strings) to a file

In [32]:
# helloFile.write('Hello!!!')
helloFile.close()

The close() Function

Call close when you are done with the file.

The shelve Module

The shelve module can store Python values in a binary file. In Windows, they're stored in files [mydata].bak, [mydata].dat and [mydata].dir

The shelve.open() Method

shelve.open() returns a dictionary-like shelf value

In [33]:
import shelve
shelfFile = shelve.open('Z:\\IT\\Python\\General Python\\Snippets\\mydata')
shelfFile['cats'] = ['Zophie', 'Pooka', 'Simon', 'Fat-tail', 'Cleo']
#shelfFile.close()
In [34]:
# shelfFile = shelve.open('mydata')
shelfFile['cats']
Out[34]:
['Zophie', 'Pooka', 'Simon', 'Fat-tail', 'Cleo']

The keys() and values() Shelf Methods

In [35]:
shelfFile.keys()
Out[35]:
KeysView(<shelve.DbfilenameShelf object at 0x041BC450>)
In [36]:
list(shelfFile.keys())
Out[36]:
['cats']
In [37]:
list(shelfFile.values())
Out[37]:
[['Zophie', 'Pooka', 'Simon', 'Fat-tail', 'Cleo']]
In [38]:
shelfFile.close()

Copying and Moving Files and Folders

You can organize the files and folders using module shutil

The copy() Method

Use shutil.copy() to copy a file

In [39]:
import shutil
# shutil.copy('c:\\spam.txt', 'c:\\delicious')
# 'c:\\delicious\\spam.txt'
In [40]:
# shutil.copy('c:\\spam.txt', 'c:\\delicious\\spamspam.txt')
# 'c:\\delicious\\spamspam.txt'

The copytree() Method

Use shutil.copytree() to copy a folder and all its contents

In [41]:
# shutil.copytree('c:\\folder', 'c:\\folder_backup')
# 'c:\\folder_backup'

The move() Method

Use shutil.move() to move a file but also rename it as well

In [42]:
# shutil.move('c:\\spam.txt', 'c:\\delicious')
# 'c:\\delicious\\spam.txt'
In [43]:
# shutil.move('c:\\spam.txt', 'c:\\delicious\\eggs.txt')
# 'c:\\delicious\\eggs.txt'

Deleting Files

os.unlink()

Use os.unlink() to delete a single file

In [44]:
import os
# os.unlink('bacon.txt')

os.rmdir()

Use os.rmdir() to delete a folder (but the folder must be empty)

In [45]:
# os.rmdir('folder1')

shutil.rmtree()

Use shutil.rmtree() to delete a folder and its entire contents

In [46]:
# shutil.rmtree('c:\\delicious')

Dry Run

Deleting can be dangerous, so do a dry run first i.e. commenting all deletion related commands and use print to see the effect first

In [47]:
import os

os.chdir('Z:\\IT\\Python\\General Python\\test')

for filename in os.listdir():
    if filename.endswith('.txt'):
        #os.unlink(filename)
        print(filename)
test.txt

The send2trash Module

To install send2trash Module:

  • Win+R to open "Run Dialog"
  • Type cmd, press Enter
  • pip install send2trash

Use send2trash.send2trash to send file or folder to the recycling bin

In [48]:
import send2trash
# send2trash.send2trash('c:\\users\\al\\desktop\\file1.txt')

Walking a Directory Tree

The os.walk() Function

The os.walk() Function returns 3 values on each iteration

In [49]:
import os
for folderName, subfolders, filenames in os.walk('Z:\\IT\\Python\\General Python\\test'):
    print('The folder is ' + folderName)
    print('The subfolders in ' + folderName + ' are:' + str(subfolders))
    print('The filenames in ' + folderName + ' are:' + str(filenames))

    for subfolder in subfolders:
        if 'New' in subfolder:
            # os.rmdir(subfolder)
            print('directory name: ' + str(subfolder))

    for file in filenames:
        if file.endswith('.txt'):
            # shutil.copy(os.path.join(folderName, file), os.path.join(folderName, file + '.backup'))
            print('file name:' + os.path.join(folderName, file))
The folder is Z:\IT\Python\General Python\test
The subfolders in Z:\IT\Python\General Python\test are:['New folder1', 'New folder2']
The filenames in Z:\IT\Python\General Python\test are:['test.txt']
directory name: New folder1
directory name: New folder2
file name:Z:\IT\Python\General Python\test\test.txt
The folder is Z:\IT\Python\General Python\test\New folder1
The subfolders in Z:\IT\Python\General Python\test\New folder1 are:[]
The filenames in Z:\IT\Python\General Python\test\New folder1 are:['New folder 1 - Copy.txt', 'New folder 1.txt']
file name:Z:\IT\Python\General Python\test\New folder1\New folder 1 - Copy.txt
file name:Z:\IT\Python\General Python\test\New folder1\New folder 1.txt
The folder is Z:\IT\Python\General Python\test\New folder2
The subfolders in Z:\IT\Python\General Python\test\New folder2 are:[]
The filenames in Z:\IT\Python\General Python\test\New folder2 are:['New folder 2.txt']
file name:Z:\IT\Python\General Python\test\New folder2\New folder 2.txt