1、獲取當(dāng)前腳本目錄
import os
py_path = os.path.dirname(__file__)
2、讀取腳本目錄下的文件
#目錄下有test.txt
filename = py_path + '/test.txt'
str = open(filename, 'r').read()
print(str)
3、文件復(fù)制到某個(gè)目錄
import shutil
def copy_file():
# 當(dāng)前目錄下的abc文件
txt_file = './abc.txt'
# 當(dāng)前目錄下的一個(gè)目錄
target_dir = './target'
# 文件復(fù)制到某個(gè)目錄下,如果目標(biāo)目錄已存在,覆蓋(未捕獲異常)
# os.path.exists判斷文件\文件夾都可以(存在的情況下,你不知道是文件,還是文件夾)
if os.path.isfile(txt_file):
shutil.copy(txt_file, target_dir)
else:
print('-->> :', "文件不存在")