微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

摆脱glob.glob中的\\

如何解决摆脱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软件包,因为它是处理路径的新常识

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。