Python编程
文件输入/输出
读取整个文件:
inputFileText = open("testit.txt", "r").read()
print(inputFileText)
在这种情况下,"r"
参数表示文件将以只读模式打开。
从文件中读取指定字节数:
inputFileText = open("testit.txt", "r").read(123)
print(inputFileText)
当打开文件时,默认从文件的开头开始读取。如果希望更随机地访问文件,可以使用 seek()
改变文件中的当前位置,使用 tell()
获取当前文件位置。下面是一个示例:
>>> f = open("/proc/cpuinfo", "r")
>>> f.tell()
0L
>>> f.read(10)
'processor\t'
>>> f.read(10)
': 0\nvendor'
>>> f.tell()
20L
>>> f.seek(10)
>>> f.tell()
10L
>>> f.read(10)
': 0\nvendor'
>>> f.close()
>>> f
<closed file '/proc/cpuinfo', mode 'r' at 0xb7d79770>
在这里,文件被打开,读取了两次十个字节,tell()
显示当前偏移量为 20,然后使用 seek()
返回到位置 10(即第二次读取开始的地方),再次读取并打印了十个字节。当不再需要对文件进行操作时,使用 close()
关闭文件。
逐行读取:
for line in open("testit.txt", "r"):
print(line)
在这种情况下,readlines()
会返回一个数组,包含文件的每一行作为数组条目。通过 readline()
函数可以逐行读取并返回当前行作为字符串。这个示例在打印每行之间会额外添加一个换行符,因为 print
会自动插入换行符。
写入文件
要向文件写入内容,需要在 open()
的第二个参数中使用 "w"
,如果文件已存在,它会覆盖文件的内容:
outputFileText = "Here's some text to save in a file"
open("testit.txt", "w").write(outputFileText)
要追加内容到文件中,需要使用 "a"
参数(表示 append):
outputFileText = "Here's some text to add to the existing file."
open("testit.txt", "a").write(outputFileText)
注意,这不会在现有文件内容和要添加的字符串之间插入换行符。
使用 with
关键字
自 Python 2.5 以来,可以使用 with
关键字确保文件句柄尽早释放,并使其具有异常安全性:
with open("input.txt") as file1:
data = file1.read()
# 处理数据
或者逐行读取:
with open("input.txt") as file1:
for line in file1:
print(line)
有关 with
关键字的更多信息,请参见 Context Managers 章节。
测试文件
检查路径是否存在:
import os
os.path.exists('<path string>')
在使用像 Microsoft Windows™ 这样的系统时,目录分隔符会与路径字符串发生冲突。为了解决这个问题,可以这样做:
import os
os.path.exists('C:\\windows\\example\\path')
更好的方法是使用 "raw" 字符串,或者 r
前缀:
import os
os.path.exists(r'C:\windows\example\path')
os.path
提供了一些其他有用的函数,os.path.exists()
仅仅确认路径是否存在。还有一些函数可以让你知道路径是文件、目录、挂载点还是符号链接。甚至还有 os.path.realpath()
函数,揭示符号链接的真实目的地:
>>> import os
>>> os.path.isfile("/")
False
>>> os.path.isfile("/proc/cpuinfo")
True
>>> os.path.isdir("/")
True
>>> os.path.isdir("/proc/cpuinfo")
False
>>> os.path.ismount("/")
True
>>> os.path.islink("/")
False
>>> os.path.islink("/vmlinuz")
True
>>> os.path.realpath("/vmlinuz")
'/boot/vmlinuz-2.6.24-21-generic'
常见文件操作
要复制或移动文件,可以使用 shutil
库:
import shutil
shutil.move("originallocation.txt", "newlocation.txt")
shutil.copy("original.txt", "copy.txt")
执行递归复制时,可以使用 copytree()
,执行递归删除时,可以使用 rmtree()
:
import shutil
shutil.copytree("dir1", "dir2")
shutil.rmtree("dir1")
要删除单个文件,可以使用 os
模块中的 remove()
函数:
import os
os.remove("file.txt")
查找文件
可以使用 glob
查找文件:
glob.glob('*.txt') # 查找当前目录中以 .txt 结尾的文件
glob.glob('*\\*.txt') # 查找当前目录下任何直接子目录中的 .txt 文件
glob.glob('C:\\Windows\\*.exe')
for fileName in glob.glob('C:\\Windows\\*.exe'):
print(fileName)
glob.glob('C:\\Windows\\**.exe', recursive=True) # Python 3.5:** 允许递归嵌套
可以使用 os.listdir()
列出目录的内容:
filesAndDirectories = os.listdir('.')
for item in filesAndDirectories:
if os.path.isfile(item) and item.endswith('.txt'):
print("Text file: " + item)
if os.path.isdir(item):
print("Directory: " + item)
获取目录中所有项目的列表,包括嵌套项目:
for root, directories, files in os.walk('/user/Joe Hoe'):
print("Root: " + root) # 例如:/user/Joe Hoe/Docs
for dir1 in directories:
print("Dir.: " + dir1) # 例如:Fin
print("Dir. 2: " + os.path.join(root, dir1)) # 例如:/user/Joe Hoe/Docs/Fin
for file1 in files:
print("File: " + file1) # 例如:MyFile.txt
print("File 2: " + os.path.join(root, file1)) # 例如:/user/Joe Hoe/Docs/MyFile.txt
上面的代码中,root
会取 /user/Joe Hoe
目录及其所有子目录的值,directories
和 files
仅包含每个根目录下的目录和文件。
使用列表推导获取目录中所有文件的列表,包括嵌套文件,并以 .txt
结尾:
files = [os.path.join(r, f) for r, d, fs in os.walk(".") for f in fs if f.endswith(".txt")]
作为迭代器:
files = (os.path.join(r, f) for r, d, fs in os.walk(".") for f in fs if f.endswith(".txt"))
当前目录
获取当前工作目录:
os.getcwd()
改变当前工作目录:
os.chdir('C:\\')