最近帮公司数据分析的同事配置代码的时候遇到一个很坑的问题,他的电脑是 windows 的,我的是 macOS 的,线上生产环境是 linux 的。他写完代码后要我这边去部署上线,然后发现其文件路径是这样的 "\Users\analysis\project" 。然后代码到了我的系统上就无法运行了,要改造这部分的代码。
from pathlib import Pathdata_folder = Path("project/resources/")file_to_open = data_folder / "sz_feb_anchor_ana.txt"with open(file_to_open) as f: for per_line in f.readlines(): print(per_line)
from pathlib import Pathfilename = Path("project/resources/sz_feb_anchor_ana.txt")print(filename.name)# prints "sz_feb_anchor_ana.txt"print(filename.suffix)# prints "txt"print(filename.stem)# prints "sz_feb_anchor_ana"if not filename.exists(): print("Oops, file doesn't exist!")else: print("Yay, the file exists!")filename.is_file()# Truefilename.is_dir()# Falsefilename.parent# PosixPath('project/resources')

好看的人都点了在看
文章转载自鸡仔说,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。






