Yiğit Çatak
Yiğit Çatak

Reputation: 31

VSCode - Python - Interactive window finds the file but main python file can not

Basically I was trying to format the .csv files I have and I was initially working on interactive window to check every step.

When I decided to run the following code in the actual .py file, which has no issue in the interactive window, it raised the following file error FileNotFoundError: [Errno 2] No such file or directory: 'bearingset/health_20_0.csv'.

I think I can just save the files from the interactive window, but I wonder what is the problem I am overlooking that it does not run in the actual code. For information my .py file is in the same folder with a folder called "bearingset" and all the .csv files are in that subfolder

# %%
import pandas as pd

nameList = ["health_20_0.csv",
            "comb_20_0.csv",
            "inner_20_0.csv",
            "outer_20_0.csv",
            "health_30_2.csv",
            "comb_30_2.csv",
            "inner_30_2.csv",
            "outer_30_2.csv",
            "ball_30_2.csv"
            ]

for fileName in nameList:
  fullName = f'"bearingset/{fileName}"'
  df = pd.read_csv(fullName)

  df = df.iloc[11:]
  df = df[df.columns[0]].apply(lambda x: [float(item) for item in x.split("\t")[0:-1]])
  df = pd.DataFrame(df.values.tolist())
  df.to_csv(fullName, index=False)

Upvotes: 0

Views: 1314

Answers (2)

Steven-MSFT
Steven-MSFT

Reputation: 8431

Problem:

I think it should be this: fullName = f"bearingset/{fileName}".

But the Error you give out has no relationship to that, It seems like the cwd caused it.

The cwd in the interactive is different from which in the python file. You can get it through this code:

import os
print(os.getcwd())

Solution:

  1. If you run it in the debug mode, you can set it in the launch.json file: "cwd": "the path to the folder"

  2. You can take advantage of the absolute path instead of the relative path.

  3. You can change the cwd in the python file: os.chdir('path of the folder')

Upvotes: 1

Sai Suman Chitturi
Sai Suman Chitturi

Reputation: 422

Try changing

fullName = f'"bearingset/{fileName}"'

to

fullName = "bearingset/" + fileName

Upvotes: 0

Related Questions