Input/Output Files#
Files and Directories#
How to write a path string in Python:
path = "C:\\myfolder\\temp" # escaped backslashes
path = "C:/myfolder/temp" # forward slashes (cross-platform)
path = r"C:\myfolder\temp" # raw string (recommended on Windows)
A string prefixed with r or R is a raw string — backslashes are treated as literal characters.
Module os#
The os module provides functions for file and directory operations.
Function |
Description |
|---|---|
|
Change the current working directory |
|
List entries in a directory |
|
Create a directory (including intermediate ones) |
|
Delete a file |
|
Delete an empty directory |
|
Rename a file or directory |
|
Return True if path exists |
|
Walk a directory tree, yielding |
# Delete everything inside a directory (use with caution!)
import os
for root, dirs, files in os.walk(r"E:\myfolder", topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
# Create a directory if it doesn't exist
import os
mydir = r"D:\temp"
if not os.path.exists(mydir):
os.makedirs(mydir)
# Delete an empty directory
import os
mydir = r"D:\temp"
if os.listdir(mydir) == []:
os.rmdir(mydir)
Module shutil#
The shutil module provides high-level file and directory operations.
Function |
Description |
|---|---|
|
Move a file or directory |
|
Copy a file (preserving permissions) |
|
Copy an entire directory tree |
|
Delete a directory tree recursively |
# Move all files from one directory to another
import shutil
import os
import glob
mydir = r"D:\workdir"
tempdir = r"D:\workdir\temp"
if not os.path.exists(tempdir):
os.makedirs(tempdir)
filelist = glob.glob(os.path.join(mydir, "*"))
for file in filelist:
shutil.move(file, tempdir)
Module zipfile#
The zipfile module provides tools to create, read, write, and extract ZIP archives.
Method |
Description |
|---|---|
|
Open or create a ZIP archive ( |
|
Add a file to the archive |
|
Extract all files to a directory |
import glob
import zipfile
mydir = r"D:\workdir"
with zipfile.ZipFile(os.path.join(mydir, "zippedfiles.zip"), "w") as zipped:
for f in glob.glob(os.path.join(mydir, "*.pdf")):
zipped.write(f, os.path.basename(f), compress_type=zipfile.ZIP_DEFLATED)
Note
Always use with statements to ensure files are properly closed.
Module glob#
The glob module finds pathnames matching a shell-style pattern.
Pattern |
Meaning |
|---|---|
|
Matches any single character |
|
Matches zero or more characters |
|
Matches any character in seq ( |
>>> import glob
>>> glob.glob(r'C:\temp\card?.gif')
['C:\\temp\\card1.gif', 'C:\\temp\\card2.gif']
>>> glob.glob(r'C:\temp\*.gif')
['C:\\temp\\1g.gif', 'C:\\temp\\card1.gif', 'C:\\temp\\card2.gif']
>>> glob.glob(r'C:\temp\[0-9]*')
['C:\\temp\\1g.gif', 'C:\\temp\\2a.txt']
Running a Python File#
# From another script (Python 3)
exec(open("C:/Temp/Test.py").read())
# From the command line
$ python C:/Temp/Test.py
Warning
execfile() was removed in Python 3. Use exec(open(...).read()) or better yet, import the script as a module.
Reading Keyboard Input#
>>> name = input("Enter your name: ")
Enter your name: Alice
>>> name
'Alice'
Note
In Python 3, input() always returns a string. Use int(input(...)) or float(input(...)) to read numbers. The raw_input() function from Python 2 was removed — input() in Python 3 behaves like Python 2’s raw_input().