python处理windows路径
在windows中用python处理文件的时候,需要输入文件路径。
在jupyter lab中使用路径
以下的写法都是正确能用的
from pathlib import Path
fpath = r'D:\next_cloud\newfile.txt'
fpath = 'D:\\next_cloud\\newfile.txt'
fpath = 'D:/next_cloud/newfile.txt'
fpath = Path(fpath)
这样就能直接使用从windows的文件管理器复制过来的路径了。
SlashPath实例化之后获得的是Path对象,不改变使用习惯。在命令行中使用路径
powershell
在powershell,包括windows powershell和powershell v7中,直接用从windows的文件管理器复制过来的路径给Path对象就行。 main.py内容如下:
main.py
在powershell中运行结果如下:
import argparse
from pathlib import Path
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("fpath")
args = parser.parse_args()
run_config_dict = vars(args)
fpath = Path(run_config_dict['fpath'])
print(fpath)
git bash
git bash会把反斜杠处理成转义符,所以在git bash中需要按下面的方式输入: