繁体   English   中英

Python - 在 windows10 中打开文件的路径中将 / 替换为 \\

[英]Python - replace / by \\ in path to open file in windows10

我有一个包含一些图像路径的列表:

Image_path_/ = os.path.join(str(folder_with_images))
Image_path_/

"('C:/Users/A/Desktop/image.tif',)"

要在 windows 10 上的ImageJ中打开 image.tif,我需要图像的路径,例如:
"C:\\Users\\A\\Desktop\\image.tif"

我想要一个自动的过程。 运行脚本时,Macros.txt 被覆盖,稍后用 ImageJ 打开
首先,我需要将上面的路径保存为 var 中的 str :
Image_path_\\ = "C:\\Users\\A\\Desktop\\image.tif"

然后,Macros.txt 被覆盖。 在下面的第二行 var Image_path_\ 应该插入

F = open(r"C:\Users\A\Desktop\Macros.txt",'w')
F.write('open(' + Image_path_\\\ + ')';\n') #equal to: F.write('open(C:\\\Users\\\A\\\Desktop\\\image.tif);\n')
F.close()

问题是我找不到用 python 写 \\ 的方法。
如何将 / 替换为 \\ 以便在变量中将其分配为 str?

我可以通过对@Maxxik CZ 的建议进行一些小的修改来解决。

path_str = str(path) path_file = path_str.replace('\\', '\\\\')

有了这个我可以编写.txt文件并插入路径+名称如果图像

F.write("open('" + path_file + '\\\\denoised_' + name_image + "');\n")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM