文件重命名:os.rename
删除文件:os.remove
创建文件夹:os.mkdir
获取当前目录:os.getcwd
删除文件夹:os.rmdir
获取当前文件:file
文件名截取:(p,name) = os.path.split()
# 文件操作
# 打开 关闭
# 读写
# 读:open('路径','模式:r,w,w+,a,a+',encoding='utf-8')
# 二进制文件:rb wb ab
# read readline readlines
# with open('路径','r',encoding='utf-8') as f:
# f.read()
#此方式不需要关闭文件
# 处理文件、文件夹。os 模块
import os
# os.mkdir('')
# os.rmdir('')
# 获取当前文件夹的绝对路径
os.getcwd()
# 获取当前文件的绝对路径
os.path.abspath(__file__)
print(__file__)
import shutil
#删除的文件夹下有文件
# shutil.rmtree('')
p = os.path.dirname(os.path.abspath(__file__))
print(os.getcwd())
print(p)
print("*"*10)
current_dir = os.getcwd()
test_file = os.path.join(current_dir,'data/test.txt')
print(test_file)
with open(test_file,'r',encoding='utf-8') as f:
data = f.readlines()
print(data,type(data))
for i in range(0,len(data)):
print(data[i])
# print(os.getcwd())
name = input()
file_name = os.path.join(os.getcwd(),'data/{}.txt'.format(name))
file_name1 = os.path.join(os.getcwd(),'data/{}副本.txt'.format(name))
with open(file_name,'r') as f:
with open(file_name1,'w') as f1:
f1.write(f.read())
# os.path.abspath(__file__)
print(__file__)