简体   繁体   中英

Getting the following Error: FileNotFoundError: [WinError 3] The system cannot find the path specified:

I am new with Python and I am attempting to extract a specific column from a csv file (Column name = "Hostname"). I keep getting error: FileNotFoundError: [WinError 3] The system cannot find the path specified :

#################################################

import os

import pandas as pd

data_location = pd.read_csv(r'C:\\Users\\Marco A\\Desktop\\csv_files\\Status_CS.csv')

df_total = pd.DataFrame(columns=["Hostname"])

for file in os.listdir(str(data_location)):

    df_file = pd.read_csv(data_location + file)
    selected_columns = df_file.loc[:, df_total]
    df_total = pd.concat([selected_columns, df_total], ignore_index=False)

df_total.to_csv("ValuableColumns.csv", index=False)

Error states its in line 10 (" for file in os.listdir(str(data_location)): ") I have also attempted the following code in line 10 " for file in os.listdir(data_location): however, I get the following error:

line 11, in for file in os.listdir(data_location): TypeError: listdir: path should be string, bytes, os.PathLike or None, not DataFrame

Not quite sure how to proceed. Please point me to the right direction.

You are reading the content of the.csv file in the variable data_location . Then, you are calling os.listdir() using the content of the.csv file, and this is not a string directory but a data frame so it will throw an error.

The variable data_location must be a string containing the path of the directory that you are trying to list its files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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