python教程四(文件)

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()))
'''
'''
方法:enumeratelist, start) 将列表或字符串组成一个索引序列,同时获得索引和值
参数:list:列表或者字符串等可迭代的对象
     start:索引起始值,默认为0,一般设为1
返回值:None
'''
'''
#获得索引和值
with open('test.txt', 'r') as file:
    for index, item in enumerate(file):  #不指定start
        print(index)
        print(type(index))#int
        print(type(item))#str
        # print(index, item)
#0 AAAAAAAAAAAAAAAAAAAAA
#1 BBBBBBBBBBBBBBBBBBBBBBB
#2 CCCCCCCCCCCCCCCCCCCCC
'''

'''
#获得索引和值
with open('test.txt', 'r') as file:
    for index, item in enumerate(file, 1):  #指定start:1
        print(index, item)
#1 AAAAAAAAAAAAAAAAAAAAA
#2 BBBBBBBBBBBBBBBBBBBBBBB
#3 CCCCCCCCCCCCCCCCCCCCC
'''
'''
######################################
写入文件
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())
#this is new sentence
'''
'''
#向文件中增加一些数据
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())
#this is new sentence
#this is also new sentence
'''
'''
#####################################
清空文件内容
方法:rem=open('test.txt', "r+")  清空txt文件的内容
     rem.truncate()
参数:要清空的文件
返回值:None
'''
'''
#先读取文件内容
with open('test.txt', 'r') as file:
    print(file.read())
    #this is new sentence
    #this is also new sentence
#清空
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)
#None
'''
'''
#删除指定路径下的文件
import os
test = os.remove('D:\\pycharmfiles\\test\\test2.jpg')
print(test)
#None
'''
'''
删除文件夹
方法:os.rmdir(dir)   要删除的文件夹必须是空文件夹
参数:要删除的文件夹或包含文件夹的路径
返回值:None
'''
'''
import os
test = os.rmdir('test\\test1')
print(test)
'''
'''
###################################
显示当前工作目录
方法:os.getcwd()
参数:None
返回值:当前工作目录
'''
'''
import os
print('当前工作目录:', os.getcwd())
#当前工作目录: D:\pycharmfiles
'''
'''
#####################################
创建目录
方法:os.mkdir(dir) 该函数将在指定的路径下创建目录
参数:目录名/指定路径+目录名
返回值:None
'''
'''
#在当前路径创建新目录
import os
test = os.mkdir('test_folder')
print(test)
#None
'''
'''
#在指定路径创建新目录
import os
test = os.mkdir('test_folder\\test')
print(test)
#None
'''
'''
创建多级目录
方法:os.makedirs(dir) 该函数将在指定的路径下创建多级目录
参数:多级目录/指定路径的多级目录
返回值:None
'''
'''
import os
test = os.makedirs('test_folder\\test\\test1')
print(test)
#None
'''

'''
#######################################
获取文件列表
方法:glob.glob(file) 获取特定目录中的文件列表
参数:要获取的文件或包含文件的路径
返回值:获取的文件相对路径列表
'''
'''
import glob
#获取特定目录下以p开头的所有文件
files = glob.glob('p*')
print(files)
#['pycharm问题.py', 'pytorch模块.py']
'''
'''
######################################
复制文件
方法:shutil.copy(old, new)  实现文件的复制old->new
参数: old:只能是文件
      new:可以是文件,也可以是目录
返回值:复制后的文件的相对路径
'''
'''
#复制文件
import shutil
test = shutil.copy('test.jpg', 'test1.jpg')
print(test)
#test1.jpg
'''
'''
#复制文件到指定目录
import shutil
test = shutil.copy('test.jpg', 'test_folder')
print(test)
#test_folder\test.jpg
'''
'''
复制目录
方法:shutil.copytree("old", "new")  复制指定目录
参数:old:只能是目录
     new:只能是目录,且必须不存在
返回值:复制后的目录的相对路径
'''
'''
import shutil
test = shutil.copytree('test_folder', 'test_folder2')
print(test)
#test_folder2
'''
'''
######################################
检查目录/文件
方法:os.path.exists(path) 检查是否存在特定路径
参数:目录或文件
返回值:布尔值
'''
'''
#检查目录是否存在
import os
test = os.path.exists('test')
print(test)
#True
'''
'''
#检查文件是否存在
import os
test = os.path.exists('test.jpg')
print(test)
#True
'''
'''
方法:os.path.isdir(dir) 检查路径是否是目录
参数:目录
返回值:布尔值
'''
'''
import os
test = os.path.isdir('test_folder')
print(test)
#True
'''
'''
方法:os.path.isfile(file)  检查路径是否是文件
参数:文件
返回值:布尔值
'''
'''
import os
test = os.path.isfile('test.jpg')
print(test)
#True
'''
'''
########################################
获取文件信息
方法:Path(file).name  提取文件名
参数:文件
返回值:文件名
'''
'''
from pathlib import Path
test = Path('test.jpg').name
print(test)
#test.jpg
'''
'''
方法:Path(file).stem  提取不带后缀的文件名
参数:文件
返回值:不带后缀的文件名
'''
'''
from pathlib import Path
test = Path('test.jpg').stem
print(test)
#test
'''
'''
方法:Path(file).suffix    单独提取文件的后缀
参数:文件
返回值:该文件的后缀
'''
'''
from pathlib import Path
test = Path('test.py').suffix
print(test)
#文件后缀: .py
'''
'''
获取文件更多信息
方法:Path(file).stat()
参数:file:文件
返回值:文件大小和修改时间信息
'''
'''
from pathlib import Path
test = Path('test.py').stat()
print(test)
print(test.st_size)# 获取文件大小,单位bytes
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()
'''
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值