Python-在Windows10中打开文件的路径中,用\\替换/ \

问题描述 投票:0回答:1

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

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?

python path slash
1个回答
-1
投票
r"C:\Users\A\Desktop\Macros.txt".replace("\\", "//")

将输出

'C://Users//A//Desktop//Macros.txt'
© www.soinside.com 2019 - 2024. All rights reserved.