[英]Get rid of \\ in glob.glob
我有以下代碼:
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',]
因此該函數運行良好,但我想擺脫“\\”並將其替換為“/”,因為我明確希望在我的函數中執行此操作。
您可以使用print([filename.replace("\\\\", "/") for filename in EDS_files_list])
而不是print(EDS_files_list)
。 這將用/
替換字符串中\\\\
所有實例,這將使其像您想要的那樣輸出。
您可以在 python3 中使用 pathlib 包,因為它是處理路徑的新常態
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.