Python文件操作常用方法,更多详细运用见python3.6标准库:https://docs.python.org/3.6/library/index.html
读取文件
with open(file, way) as file,从文件中读取数据,默认读取模式打开文件,并将文件中的数据视为文本
方法:file.read() 文件中数据少,使用read()一次读取所有数据;数据很多,使用enumerate()逐行处理
参数:file:要读取的文件
way:读取方式,默认‘r’
返回值:文件中的数据(字符串)
with open('test.txt', 'r') as file:
print(file.read())
print(type(file.read()))
'''
'''
方法:enumerate(list, start) 将列表或字符串组成一个索引序列,同时获得索引和值
参数:list:列表或者字符串等可迭代的对象
start:索引起始值,默认为0,一般设为1
返回值:None
'''
'''
with open('test.txt', 'r') as file:
for index, item in enumerate(file):
print(index)
print(type(index))
print(type(item))
'''
'''
with open('test.txt', 'r') as file:
for index, item in enumerate(file, 1):
print(index, item)
'''
'''
写入文件
with open(file, way) as file,将way改为w/a打开文件来创建文件对象
方法:file.write(data)
参数: data:需要写入文件的数据(字符串)
file:文件
way:'w',覆盖旧数据写入新数据;'a',在原有数据基础上增加新数据
返回值:None
'''
'''
with open('test.txt', 'w') as file:
test = 'this is new sentence'
file.write(test)
with open('test.txt', 'r') as file:
print(file.read())
'''
'''
with open('test.txt', 'a') as file:
test = '\nthis is also new sentence'
file.write(test)
with open('test.txt') as file:
print(file.read())
'''
'''
清空文件内容
方法:rem=open('test.txt', "r+") 清空txt文件的内容
rem.truncate()
参数:要清空的文件
返回值:None
'''
'''
with open('test.txt', 'r') as file:
print(file.read())
rem=open('test.txt', "r+")
rem.truncate()
with open('test.txt', 'r') as file:
print(file.read())
'''
'''
删除文件
方法:os.remove(file)
参数:要删除的文件或包含文件的路径
返回值:None
'''
'''
import os
test = os.remove('test1.jpg')
print(test)
'''
'''
import os
test = os.remove('D:\\pycharmfiles\\test\\test2.jpg')
print(test)
'''
'''
删除文件夹
方法:os.rmdir(dir) 要删除的文件夹必须是空文件夹
参数:要删除的文件夹或包含文件夹的路径
返回值:None
'''
'''
import os
test = os.rmdir('test\\test1')
print(test)
'''
'''
显示当前工作目录
方法:os.getcwd()
参数:None
返回值:当前工作目录
'''
'''
import os
print('当前工作目录:', os.getcwd())
'''
'''
创建目录
方法:os.mkdir(dir) 该函数将在指定的路径下创建目录
参数:目录名/指定路径+目录名
返回值:None
'''
'''
import os
test = os.mkdir('test_folder')
print(test)
'''
'''
import os
test = os.mkdir('test_folder\\test')
print(test)
'''
'''
创建多级目录
方法:os.makedirs(dir) 该函数将在指定的路径下创建多级目录
参数:多级目录/指定路径的多级目录
返回值:None
'''
'''
import os
test = os.makedirs('test_folder\\test\\test1')
print(test)
'''
'''
获取文件列表
方法:glob.glob(file) 获取特定目录中的文件列表
参数:要获取的文件或包含文件的路径
返回值:获取的文件相对路径列表
'''
'''
import glob
files = glob.glob('p*')
print(files)
'''
'''
复制文件
方法:shutil.copy(old, new) 实现文件的复制old->new
参数: old:只能是文件
new:可以是文件,也可以是目录
返回值:复制后的文件的相对路径
'''
'''
import shutil
test = shutil.copy('test.jpg', 'test1.jpg')
print(test)
'''
'''
import shutil
test = shutil.copy('test.jpg', 'test_folder')
print(test)
'''
'''
复制目录
方法:shutil.copytree("old", "new") 复制指定目录
参数:old:只能是目录
new:只能是目录,且必须不存在
返回值:复制后的目录的相对路径
'''
'''
import shutil
test = shutil.copytree('test_folder', 'test_folder2')
print(test)
'''
'''
检查目录/文件
方法:os.path.exists(path) 检查是否存在特定路径
参数:目录或文件
返回值:布尔值
'''
'''
import os
test = os.path.exists('test')
print(test)
'''
'''
import os
test = os.path.exists('test.jpg')
print(test)
'''
'''
方法:os.path.isdir(dir) 检查路径是否是目录
参数:目录
返回值:布尔值
'''
'''
import os
test = os.path.isdir('test_folder')
print(test)
'''
'''
方法:os.path.isfile(file) 检查路径是否是文件
参数:文件
返回值:布尔值
'''
'''
import os
test = os.path.isfile('test.jpg')
print(test)
'''
'''
获取文件信息
方法:Path(file).name 提取文件名
参数:文件
返回值:文件名
'''
'''
from pathlib import Path
test = Path('test.jpg').name
print(test)
'''
'''
方法:Path(file).stem 提取不带后缀的文件名
参数:文件
返回值:不带后缀的文件名
'''
'''
from pathlib import Path
test = Path('test.jpg').stem
print(test)
'''
'''
方法:Path(file).suffix 单独提取文件的后缀
参数:文件
返回值:该文件的后缀
'''
'''
from pathlib import Path
test = Path('test.py').suffix
print(test)
'''
'''
获取文件更多信息
方法:Path(file).stat()
参数:file:文件
返回值:文件大小和修改时间信息
'''
'''
from pathlib import Path
test = Path('test.py').stat()
print(test)
print(test.st_size)
print(test.st_mtime)
print(test.st_atime)
'''
'''
压缩文件
from zipfile import ZipFile
with ZipFile(zip, way) as file,zipfile模块提供了文件压缩的功能
方法:ZipFile(zip,way),创建一个zip文件对象
file.write(file),添加文件到压缩包
参数:zip:压缩包名
way:压缩方式,一般为‘w’
file:要添加到压缩包的文件
返回值:None
'''
'''
from zipfile import ZipFile
创建压缩文件
with ZipFile('test.zip', 'w') as file:
test = 'test.txt'
file.write(test)
'''
'''
解压文件
from zipfile import ZipFile
with ZipFile(zip) as zip_file 同理
方法:ZipFile(zip),获取一个压缩文件
zip_file.printdir(),输出此压缩文件包中所有文件的名称,修改时间,文件大小
zip_file.extractall(),解压所有文件到当前目录
参数:压缩文件
返回值:None
'''
'''
from zipfile import ZipFile
with ZipFile('test.zip') as zip_file:
zip_file.printdir()
zip_file.extractall()
'''