In [1]:
name = '2017-02-03-file-operations'
title = 'Standard library: file and directory operations'
tags = 'basics'
author = 'Denis Sergeev'
In [2]:
from nb_tools import connect_notebook_to_post
from IPython.core.display import HTML, Image
html = connect_notebook_to_post(name, title, tags, author)
Today we got acquianted with the following parts of Python's standard library:
os - Operating system functionalitypathlib and path.py - system paths as objectglob - Unix style pathname pattern expansiontempfile - dealing with temporary files
In [3]:
import os
Print all environment variables:
os.environ.keys()
Get a particular variable by its name from the os.environ dictionary:
In [4]:
os.environ['USER']
Out[4]:
In [5]:
os.getenv('MODEL')
You can use os.system, which returns a status code of the command execution (0 if the command did not fail):
In [6]:
os.system('ls') # by default, the output goes to the console connected to the notebook
Out[6]:
In [7]:
import subprocess as sb
In [8]:
out = sb.check_output(['ls', '-la'])
In [9]:
# print(out.decode())
In [10]:
os.getcwd()
Out[10]:
In [11]:
os.curdir
Out[11]:
In [12]:
[i for i in os.listdir() if os.path.isfile(i)]
Out[12]:
Create a directory in the current working directory
In [13]:
os.mkdir('junk_folder')
Test if it's there:
In [14]:
'junk_folder' in os.listdir(os.curdir)
Out[14]:
Rename it:
In [15]:
os.rename('junk_folder', 'foodir') # works for files as well
Delete it:
In [16]:
os.rmdir('foodir')
Create a directory and intermediate ones:
In [17]:
os.makedirs('./aaa/bbb/ccc', exist_ok=True)
In [18]:
with open('junk_file.txt', 'w') as f:
f.write('blah')
In [19]:
a = os.path.abspath('junk_file.txt')
In [20]:
a
Out[20]:
In [21]:
os.path.dirname(a)
Out[21]:
In [22]:
os.path.basename(a)
Out[22]:
In [23]:
os.path.splitext(os.path.basename(a))
Out[23]:
In [24]:
os.path.exists(os.path.dirname(a))
Out[24]:
In [25]:
os.path.isfile('junk_file.txt')
Out[25]:
In [26]:
os.path.isdir('junk_file.txt')
Out[26]:
In [27]:
os.path.expanduser('~/UEA')
Out[27]:
In [28]:
os.path.join(os.path.expanduser('~'), 'UEA', 'lalala')
Out[28]:
In [29]:
os.path.getctime(a)
Out[29]:
In [30]:
os.path.getsize(a)
Out[30]:
In [31]:
os.path.commonprefix(['~/UEA/temp_data/', '~/UEA/PUG/'])
Out[31]:
Uncomment to list the current directory tree
In [32]:
# for i in os.walk('.'):
# print(i)
The glob module provides convenient file pattern matching.
For example, you can find all files ending in '.ipynb':
In [33]:
import glob
In [34]:
glob.glob('*.ipynb')
Out[34]:
The shutil provides useful file operations:
shutil.rmtree: Recursively delete a directory tree.shutil.move: Recursively move a file or directory to another location.shutil.copy: Copy files or directories.Not a part of the standard library!
Although there is pathlib module with almost the same functionality.
In [35]:
try:
from path import Path
except ImportError:
from pathlib import Path
examples
In [36]:
p = Path(os.getenv('HOME'))
In [37]:
p
Out[37]:
In [38]:
path_to_file = p / 'UEA' / 'PUG' / 'blabla' / 'lalala'
In [39]:
path_to_file.exists()
Out[39]:
In [40]:
try:
path_to_file.makedirs_p()
except:
pass
In [41]:
path_to_file.parent.parent.glob('*.txt')
Out[41]:
Useful if your program produces some intermediate output that you can delete afterwards - temporary files are removed automatically.
In [42]:
import tempfile
Example:
In [43]:
with tempfile.TemporaryDirectory() as tmpdirname:
print('created temporary directory', tmpdirname)
In [44]:
HTML(html)
Out[44]: