简体   繁体   English

os.path和dataframe.to_csv找不到Python路径字符串

[英]Python path string not found by os.path and dataframe.to_csv

I try to sort some files, hence I first read all csv-files in a directory, sort them and then save them again in a different directory path. 我尝试对某些文件进行排序,因此我首先读取目录中的所有csv文件,对它们进行排序,然后再次将它们保存在其他目录路径中。 However, there seems to be an issue with creating the path strings because they can not be processed anymore by the libraries. 但是,创建路径字符串似乎存在问题,因为库无法再处理它们。

The following code results in an error: 'FileNotFoundError: [WinError 3] Das System kann den angegebenen Pfad nicht finden: 'C:/Users/user/Documents/Daten/NoEncaps/4-point-empty-pcb/sorted/T10/emptyPCB-T30RH30''. 下面的代码导致错误:'FileNotFoundError:[WinError 3]在系统目录中查找:'C:/ Users / user / Documents / Daten / NoEncaps / 4-point-empty-pcb / sorted / T10 /空PCB-T30RH30''。 You might ask yourself why the code is so strange: I already tried to fix it. 您可能会问自己为什么代码如此奇怪:我已经尝试对其进行修复。 For example, before there were backslashes (\\) instead of slashes - if I printed my outputpath to the console everything was fine (C:\\Users\\user...) but as soon as I attempted to use os.mkdir(...) or dataframe.to_csv(...) It gave the same error message justwith all backslashes being replaced by double backslashes. 例如,在使用反斜杠(\\)而不是斜杠之前-如果我将输出路径打印到控制台,一切都很好(C:\\ Users \\ user ...),但是当我尝试使用os.mkdir(。 。)或dataframe.to_csv(...)它给出了相同的错误消息,只是所有反斜杠都被双反斜杠替换了。 Deleting the part with the mkdir does not help as well. 用mkdir删除部件也无济于事。

Thanks for all constructive feedback in advance! 预先感谢所有建设性的反馈!

import os
import pandas as pd
import pathlib as p

path = r"C:\Users\user\Documents\Daten\NoEncaps\4-point-empty-pcb"
path_file = r"C:\Users\user\Documents\Daten\NoEncaps\4-point-empty-pcb\emptyPCB-T30RH30\Transfer_emptyPCB_C2 PLSTA T101__T_RT_2018-07-09 11_28_30_(1.1).csv"

(head,tail) = os.path.split(path_file)


#data = pd.read_csv(path_file, sep = ';')
d = {'FETname':['C2 PLSTA T10']}
data = pd.DataFrame(data=d)


head = head.replace(path,'')
head = head.replace('\\','')
fetname = data[['FETname']].loc[0].to_string()
groupname = ''
if 'C2 PLSTA T10' in fetname:
    groupname = 'T10'

outputpath = os.path.join(path,"sorted",groupname,head)
fullout = os.path.join(outputpath,tail)
outputpath = p.posixpath.join(*outputpath.split('\\'))
fullout = p.posixpath.join(*fullout.split('\\'))
print(fullout)
if not os.path.exists(outputpath):
    os.mkdir(outputpath)

data.to_csv(fullout, sep = ';')

You'll need to use os.makedirs() to create trees of directories; 您将需要使用os.makedirs()创建目录树。 os.mkdir() can only create them one directory deep, as it were. os.mkdir()只能在一个目录深处创建它们。

Maybe something like this? 也许是这样的吗? I've changed the backslashes to forward slashes, as Python is rather graceful about those cross-platform. 我已经将反斜杠更改为正斜杠,因为Python对于那些跨平台相当宽容。

import os.path

root_path = r"C:/Users/user/Documents/Daten/NoEncaps/4-point-empty-pcb"
# NB: this is relative to `root_path`.
path_file = r"emptyPCB-T30RH30/Transfer_emptyPCB_C2 PLSTA T101__T_RT_2018-07-09 11_28_30_(1.1).csv"

head, tail = os.path.split(path_file)

data = pd.read_csv(os.path.join(root_path, path_file))
fetname = data[['FETname']].loc[0].to_string()

# Generate the path out of components...
components = ['sorted']

if 'C2 PLSTA T10' in fetname:
    components.append(groupname)

components.append(head)
output_dir = os.path.join(*[root_path] + components)

if not os.path.isdir(output_dir):
    print("Creating", output_dir)
    os.makedirs(output_dir)

output_file = os.path.join(output_dir, tail)

print("Writing to", output_file)

data.to_csv(fullout, sep = ';')

as i see, the problem in string of path. 如我所见,路径字符串中的问题。
replace "\\" to "\\\\" 将“ \\”替换为“ \\\\”
it could help because the character "\\" using for format string 这可能会有所帮助,因为字符“ \\”用于格式字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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