python3 获取当前路径及os.path.dirname的使用
方法一:
import os
os.getcwd() #直接获取结果
import os
开发环境
//RUNSTATUS = "dev"
测试环境
//RUNSTATUS = "test"
线上环境
//RUNSTATUS = "online" # 线上环境
SYSTEM_STATUS = True if "windows" in platform.platform().lower() else False # 操作系统
if SYSTEM_STATUS:
txt_file_name = os.getcwd() + \'data\app\spuid.txt\'
else:
txt_file_name = os.getcwd() + \'data/app/spuid.txt\'
print(txt_file_name)
with open(txt_file_name, \'r\', encoding=\'utf8\') as fr:
data = fr.read()
sp_list = data.split(\',\')
print("sp_list)
for id in sp_list:
print(id)
方法二:
import os
os.path.dirname(os.path.realpath(\'file\'))#注意:添加单引号
python3 获取当前路径及sys.path.dirname的使用
方法一:
from os.path import dirname, realpath
import sys
sys.path.insert(0, dirname(dirname(realpath(file))))
方法二:
from os.path import dirname, realpath
cookie_path = dirname(dirname(dirname(realpath(\'file\')))) + \'/cookies/info.txt\'
with open(cookie_path, \'r\', encoding=\'utf8\') as fr:
data = fr.read()
data_dict = json.loads(data)