文件重命名:os.rename
刪除文件:os.remove
創(chuàng)建文件夾:os.mkdir
獲取當(dāng)前目錄:os.getcwd
刪除文件夾:os.rmdir
獲取當(dāng)前文件:file
文件名截?。?p,name) = os.path.split()
# 文件操作
# 打開 關(guān)閉
# 讀寫
# 讀:open('路徑','模式:r,w,w+,a,a+',encoding='utf-8')
# 二進(jìn)制文件:rb wb ab
# read readline readlines
# with open('路徑','r',encoding='utf-8') as f:
# f.read()
#此方式不需要關(guān)閉文件
# 處理文件、文件夾。os 模塊
import os
# os.mkdir('')
# os.rmdir('')
# 獲取當(dāng)前文件夾的絕對(duì)路徑
os.getcwd()
# 獲取當(dāng)前文件的絕對(duì)路徑
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__)