我有以下代码:
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)在输出中我得到:
['X:/data/Folder\\afilewithIDnumber.txt','X:/data/Folder\\anotherfilewithIDnumber.txt',]因此,该函数运行良好,但我希望去掉"\“,并将其替换为"/”,就像我在函数中明确希望做的那样。
发布于 2020-09-08 20:35:36
您可以使用print([filename.replace("\\", "/") for filename in EDS_files_list])而不是print(EDS_files_list)。这将将字符串中的所有\\实例替换为/,这将使其输出符合您的要求。
发布于 2020-09-08 20:38:46
您可以在python3中使用路径库包,因为它是处理路径的新常态。
https://stackoverflow.com/questions/63798640
复制相似问题