简体   繁体   English

去掉 glob.glob 中的 \\\\

[英]Get rid of \\ in glob.glob

I have the following code:我有以下代码:

ctypes.windll.user32.MessageBoxW(0, "Please choose a file directory", "File directory", 1)
EDS_database_path = filedialog.askdirectory()
EDS_answer = simpledialog.askstring("Input", "Please enter the ID")

EDS_files_list = glob.glob(EDS_database_path+'/**'+EDS_answer+'**', recursive = True)

print(EDS_files_list)

In the output I get:在输出中我得到:

['X:/data/Folder\\afilewithIDnumber.txt','X:/data/Folder\\anotherfilewithIDnumber.txt',]

So the function worked well but I want to get rid of "\\" and replace it with "/" as I explicitly wanted to do in my function.因此该函数运行良好,但我想摆脱“\\”并将其替换为“/”,因为我明确希望在我的函数中执行此操作。

You can use print([filename.replace("\\\\", "/") for filename in EDS_files_list]) instead of print(EDS_files_list) .您可以使用print([filename.replace("\\\\", "/") for filename in EDS_files_list])而不是print(EDS_files_list) This will replace all instances of \\\\ in the strings with / , which will make it output like you want it to.这将用/替换字符串中\\\\所有实例,这将使其像您想要的那样输出。

您可以在 python3 中使用 pathlib 包,因为它是处理路径的新常态

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

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