If we want to operate on files and directories, we can enter various commands provided by the operating system in the command line to accomplish this. For example, commands like dir, cp, and so on.
What if we want to perform these directory and file operations in a Python program? In fact, the commands provided by the operating system simply call the interface functions offered by the operating system, and Python’s built-in os module can also directly invoke these operating system interface functions.
Open the Python interactive command line, and let’s see how to use the basic functions of the os module:
>>> import os
>>> os.name # Operating system type
'posix'
If the result is posix, it indicates the system is Linux, Unix, or macOS; if it is nt, the system is Windows.
To obtain detailed system information, you can call the uname() function:
>>> os.uname()
posix.uname_result(sysname='Darwin', nodename='MichaelMacPro.local', release='14.3.0', version='Darwin Kernel Version 14.3.0: Mon Mar 23 11:59:05 PDT 2015; root:xnu-2782.20.48~5/RELEASE_X86_64', machine='x86_64')
Note that the uname() function is not available on Windows, meaning some functions of the os module are operating system-dependent.
All environment variables defined in the operating system are stored in the os.environ variable, which can be viewed directly:
>>> os.environ
environ({'VERSIONER_PYTHON_PREFER_32_BIT': 'no', 'TERM_PROGRAM_VERSION': '326', 'LOGNAME': 'michael', 'USER': 'michael', 'PATH': '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/mysql/bin', ...})
To get the value of a specific environment variable, call os.environ.get('key'):
>>> os.environ.get('PATH')
'/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/mysql/bin'
>>> os.environ.get('x', 'default')
'default'
Functions for operating files and directories are partly located in the os module and partly in the os.path module—this is something to note. To view, create, and delete directories, you can make the following calls:
# View the absolute path of the current directory:
>>> os.path.abspath('.')
'/Users/michael'
# Create a new directory under a certain directory; first represent the full path of the new directory:
>>> os.path.join('/Users/michael', 'testdir')
'/Users/michael/testdir'
# Then create the directory:
>>> os.mkdir('/Users/michael/testdir')
# Delete a directory:
>>> os.rmdir('/Users/michael/testdir')
When combining two paths, do not concatenate strings directly; instead, use the os.path.join() function, which correctly handles path separators for different operating systems. On Linux/Unix/Mac, os.path.join() returns a string like this:
part-1/part-2
While on Windows, it returns:
part-1\part-2
Similarly, when splitting a path, do not split the string directly; use the os.path.split() function instead. This splits a path into two parts, where the latter part is always the last-level directory or filename:
>>> os.path.split('/Users/michael/testdir/file.txt')
('/Users/michael/testdir', 'file.txt')
os.path.splitext() allows you to get the file extension directly, which is very convenient in many cases:
>>> os.path.splitext('/path/to/file.txt')
('/path/to/file', '.txt')
These path merging and splitting functions do not require the directories or files to actually exist; they only operate on strings.
File operations use the following functions. Assume there is a test.txt file in the current directory:
# Rename a file:
>>> os.rename('test.txt', 'test.py')
# Delete a file:
>>> os.remove('test.py')
Surprisingly, the function for copying files does not exist in the os module! The reason is that copying files is not a system call provided by the operating system. Theoretically, we can complete file copying by reading and writing files (as covered in the previous section), but this would require writing a lot more code.
Fortunately, the shutil module provides the copyfile() function. You can also find many practical functions in the shutil module, which can be regarded as a supplement to the os module.
Finally, let’s see how to use Python’s features to filter files. For example, to list all directories in the current directory, we only need one line of code:
>>> [x for x in os.listdir('.') if os.path.isdir(x)]
['.lein', '.local', '.m2', '.npm', '.ssh', '.Trash', '.vim', 'Applications', 'Desktop', ...]
To list all .py files, it also only takes one line of code:
>>> [x for x in os.listdir('.') if os.path.isfile(x) and os.path.splitext(x)[1]=='.py']
['apis.py', 'config.py', 'models.py', 'pymonitor.py', 'test_db.py', 'urls.py', 'wsgiapp.py']
Isn’t that very concise?
Python’s os module encapsulates operating system directory and file operations. Note that some of these functions are in the os module, while others are in the os.path module.
os module to write a program that can achieve the output of dir -l.do_dir
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
import os
pwd = os.path.abspath(".")
print(" Size Last Modified Name")
print("------------------------------------------------------------")
for f in os.listdir(pwd):
fsize = os.path.getsize(f)
mtime = datetime.fromtimestamp(os.path.getmtime(f)).strftime("%Y-%m-%d %H:%M")
flag = "/" if os.path.isdir(f) else ""
print("%10d %s %s%s" % (fsize, mtime, f, flag))