python读取文件路径报invalid_在Python中打开路径时出现问题

博主在使用pandas尝试从CSV文件中读取参数时遇到问题,当尝试通过变量组合路径来读取内部网络中的文件时,出现ValueError: Invalid file path or buffer object type。解决方案是将DataFrame中的路径列转换为字符串类型。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

I am writing a Python code using pandas that will open a .csv file and read some parameters that will be used later as input for another module. Along the parameters the code must read, there are locations (paths) of other .csv files in my internal network that contain data that must be incorporated later on to the final output. My problem is opening those files; unless I define explicitly the path (instead of using a reference variable that will allow me to loop over all the .csv files my final code needs), I get the ValuError: Invalid file path or buffer object type: .

I tried adding single and double quotation marks to the paths, but that didn't help. Can somebody help me to figure out what I am doing wrong?

Below are pieces of my code that hopefuly will help to clarify the issue.

Thanks in advance for your help!

Root_path =

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用Python的csv模块和os模块来读取CSV文件并将其分类到相应文件夹。以下是一个示例代码: ```python import csv import os def classify_csv_to_folders(csv_file, folder_mapping): with open(csv_file, 'r') as file: reader = csv.reader(file) next(reader) # 跳过标题行 for row in reader: filename = row[0] # 假设CSV的第一列是文件名 category = row[1] # 假设CSV的第二列是分类 if category in folder_mapping: folder = folder_mapping[category] destination_folder = os.path.join(os.getcwd(), folder) if not os.path.exists(destination_folder): os.makedirs(destination_folder) source_path = os.path.join(os.getcwd(), filename) destination_path = os.path.join(destination_folder, filename) os.rename(source_path, destination_path) print(f"Moved {filename} to {destination_folder}") else: print(f"Invalid category: {category}") # 定义分类与文件夹的映射关系 folder_mapping = { 'category1': 'folder1', 'category2': 'folder2', 'category3': 'folder3' } # 读取CSV文件并分类到相应文件夹 csv_file = 'data.csv' # 替换为您的CSV文件路径 classify_csv_to_folders(csv_file, folder_mapping) ``` 在上述代码,您需要将`csv_file`变量替换为您的CSV文件路径,并根据自己的需求定义分类与文件夹的映射关系`folder_mapping`。然后,代码将遍历CSV文件的每一行,将文件移动到相应的文件夹。 请注意,这段代码假设CSV文件的第一列是文件名,第二列是分类。您可以根据实际情况进行修改。此外,代码还会检查分类是否在`folder_mapping`定义,如果不在,则会输出相应的警告信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值