简体   繁体   中英

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) . This will replace all instances of \\\\ in the strings with / , which will make it output like you want it to.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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