【问题标题】:Get rid of \\ in glob.glob摆脱 glob.glob 中的 \\
【发布时间】:2020-09-08 17:14:30
【问题描述】:

我有以下代码:

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',]

因此该函数运行良好,但我想摆脱“\”并将其替换为“/”,因为我明确希望在我的函数中执行此操作。

【问题讨论】:

  • 不确定是否可以修改 glob 的行为,但您可以轻松地使用例如[filename.replace('\\', '/') for filename in EDS_files_list]

标签: python path glob


【解决方案1】:

您可以使用print([filename.replace("\\", "/") for filename in EDS_files_list]) 代替print(EDS_files_list)。这会将字符串中的所有\\ 实例替换为/,这将使其按您想要的方式输出。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2012-02-17
      • 2018-01-06
      • 1970-01-01
      • 1970-01-01
      • 2010-11-03
      • 2012-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多