python - for循环中文件目录中的反斜杠加倍
问题描述
所以我试图用这个循环做的是,当我循环浏览具有特定扩展名的文件时,我从这些文件中提取我想要的信息并保存它们。我的循环适用于第一个文件。但是在第一个文件之后我收到一条错误消息。我知道目录路径不存在,因为由于某种原因它使 \ 的 about 加倍,如下图所示。我如何摆脱反斜杠的两倍?
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)
推荐阅读
- strapi - STRAPI:运行命令开发时出错:未定义
- c++ - Makefile 中的 C++ 未定义引用
- time-series - ARIMA 非常适合,但在预测中返回“Nan”
- or-tools - 带有可替代物品的多个背包
- azure - 如何在 Azure 管道上运行空手道 API 测试
- java - java.io.FileNotFoundException,mkdir 不能正常工作?
- firebase - '类型'_InternalLinkedHashMap
' 不是类型 'Map 的子类型 ' ' 是我收到的错误 - git - 如何解决 bbdiff 未被用作我的 git difftool(在 macOS 上)的问题?
- java - 将图像上传到谷歌存储桶不再将其识别为图像
- java - 如何故意从服务器获取 409 HTTP 冲突响应?