![](/img/trans.png)
[英]Why does my __file__ path contain both forward slashes and back slashes?
[英]Back slashes doubling in file directory in for loop
所以我試圖用這個循環做的是,當我循環瀏覽具有特定擴展名的文件時,我從這些文件中提取我想要的信息並保存它們。 我的循環適用於第一個文件。 但是我在第一個文件后收到一條錯誤消息。 我知道目錄路徑不存在,因為出於某種原因,它是 \\ 的兩倍,如下圖所示。 如何擺脫反斜杠的加倍?
import os
import pandas as pd
import xml.etree.ElementTree as ET
#This variable was created to save time by staring it at the WI folder and go from there.
current_dur = r'Workplace Investing'
#logic to search through the directories.
for root, dirs, files in os.walk(current_dur):
for file in files:
if file.endswith('.ldm') or file.endswith('.cdm') or file.endswith('.pdm'):
full_file_name = "'" + os.path.join(root, file) + "'"
print(full_file_name)
print(os.path.join(root,file))
#for i in file_results:
#WE are parseing it.
tree = ET.parse(full_file_name)
#We then get the root.
gotten_root = tree.getroot()
.....
問題不在於雙反斜杠,因為路徑之前的 'r' 正確地完成了轉義它們的工作。 問題出在'
.
嘗試僅使用
full_file_name = os.path.join(root, file)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.