文章目录

  • os 模块
  • os.path 模块
  • 其他 os 模块
  • 文件的基本处理
  • 读取文件
  • 复制文件并重命名
  • csv 文件处理
  • excel 文件处理


os 模块

参考:https://www.runoob.com/python/os-file-methods.html

os.path 模块

os.path 模块主要用于文件的属性获取,使用前导入:

import os

官方文档:os.path — Common pathname manipulations

处理wrfout文件的Python库 python文件处理模块_重命名


============================================

需求一:对文件路径的处理

# 获取文件路径
os.path.dirname("D:/data/read/train.txt")
>>> D:/data/read

# 获取文件绝对路径
os.path.abspath("./test.py")
>>> D:/data/read/test.py

# 获取当前执行文件的绝对路径
os.path.abspath(__file__)
>>> D:/data/read/test.py

# 获取当前执行文件的绝对路径(不包含脚本名)
os.path.dirname(os.path,abspath(__file__))
>>> D:/data/read

# 获取当前执行文件所在的上一级目录
os.path.dirname(os.path.abspath(__file__)).split('/')[-1]
>>> read/

# 获取文件名
os.path.basename("D:/data/read/train.txt")
>>> train.txt

# 合并路径
p1 = 'data'
p2 = 'read'
p3 = 'train.txt'
path = os.path.join(p1, p2, p3)
>>> data/read/train.txt

# 分割路径和文件名
os.path.split("D:/data/read/train.txt")
>>> ('D:/data/read', 'train.txt')
  • dirname() 的返回值是文件的目录,但不包括文件名,其作用相当于 os.path.split(file_path)[0]
  • abspath() 获取的绝对路径是包含脚本文件名在内的,和 dirname 搭配食用,可以获取当前脚本路径
  • join() 的返回值是多个字符串的连接结果,仍为路径格式

需求二:对文件本身的处理

# 判断某路径是否为文件
os.path.isfile("D:/data/read/train.txt")
>>> True
os.path.isfile("D:/data/read")
>>> False

# 判断某文件是否存在
os.path.exists("D:/data/read/train.txt")
>>> True
os.path.exists("D:/data/read/val.txt")
>>> False

其他 os 模块

方法

描述

os.listdir(path)

返回 path 指定的文件夹下包含的文件或文件夹的名称列表

os.makedir(path)

创建名为 path 的文件夹

需求一:判断文件夹是否存在并创建

在写代码的时候,很常用的一个自定义函数就是创建文件夹。可以先用 os.path.exits() 判断是否存在目标文件,如果不存在则用 os.makedirs() 进行创建:

import os
def check_and_create(dir):
	isExists = os.path.exists(dir)
	if not isExists:
	    os.makedirs(dir)

需求二:文件或目录的重命名

os.rename(src, dst)

作用:将目录 src 重命名为 dst

实际使用时,经常会用到为目录下的文件进行 批量重命名,可以使用 for 循环实现,比如要将文件按照顺序编排为 0001.jpg0002.jpg 等等,代码实现为:

import os
path = '/your/path/'
count = 1
for file in os.listdir(path):
    os.rename(os.path.join(path,file),os.path.join(path,str(count).zfill(4)+".jpg")
    count+=1

NOTE:这里的 .zfill(4) 用于格式化字符串长度。


文件的基本处理

读取文件

复制文件并重命名

shutil.copyfile(old_file_path, new_file_path)

需求二:按行读写文件

file = open("sample.txt")
# 读文件
for line in file:
	pass # do something
file.close()
# 写文件
if os.path.exists(file):
	os.move(file)
new_file = open(file, 'w')
new_file.write() # 写入
new_file.close()
  • open() 中 w 表示覆盖写,如果要循环写入文件使用 a

需求三:修改文件内容

1. 直接修改原文件:按行读取目标文件中的内容,查找想要替换的字符串并对其进行替换,下面的代码会直接在原始文件上进行修改,建议先备份原始文件。

def alter(file ,old_str, new_str):
	content = ""
	with open(file, "r", encoding="utf-8") as fr:
		for line in fr:
			if old_str in line:
				line = line.replace(old_str,new_str)
			content += line
		with open(file, "w", encoding="utf-8") as fw:
		fw.write(content)

2. 查找原文件要修改的部份并写入新文件:按行读取原文件中的内容并写入新文件,如果找到了想要替换的字符串,则先进行替换再写入。

def copy_and_alter(file, old_str, new_str):
	with open(file, "r", encoding="utf-8") as fr,open("%s.copy" % file, "w", encoding="utf-8") as fw:
		for line in fr:
			if old_str in line:
				line = line.replace(old_str, new_str)
			fw.write(line)

需求四:对文件内容的操作

# 去掉行尾的换行符
line = line.strip('\n')

csv 文件处理

使用前导入:

import csv

需求一:读写 csv 文件

with open('DSC.csv', 'r') as csvfile:
    reader = csv.reader(csvfile)
    for line in reader:
        print(line)

>>> help(csv.reader)

打印出来的结果是数组类型,有文件中几行数据就打印几个数组,不区分表头和值

writer = csv.writer(file('data.csv','wb'))
writer.writerow(['Column1','Column2','Column3']) 

>>> help(csv.writer)

excel 文件处理

使用前导入:

conda install xlrd
conda install xlwt

Excel文件内容:

处理wrfout文件的Python库 python文件处理模块_DSC_02

'''
Excel文件的基本处理
'''
import xlrd
import xlwt

# 打开 Excel文件
workbook = xlrd.open_workbook('info.xlsx')
# 获取文件中的表单名称
name = workbook.sheet_names()
# 通过索引打开表单
sheet1 = workbook.sheet_by_index(0)
# 通过表单名称打卡表单
sheet2 = workbook.sheet_by_name('DSC')
print(sheet1, sheet2) # 可以看出用索引获取和用名称都可以获得目标sheet

# 获取表单行数
nrows1 = sheet1.nrows
nrows2 = sheet2.nrows  # nrows1=nrows2
# 获取表单列数
ncols1 = sheet1.ncols
ncols2 = sheet2.ncols  # ncols1=nclos2
print(nrows1, ncols1, nrows2, ncols2)

# 按行获取表单内容并保存在矩阵中
Mat = np.zeros((8,30))
for i in range(nrows1):
rows = sheet1.row_values(i)
DSC[i][:] = rows
# 让数据在矩阵中按列存储
DSC = DSC.T