1

I am using Python in PyCharm to run a script. After I read the excel csv file which gets read I get this error:

Traceback (most recent call last):
  File "C:\Users\lolaX\PycharmProjects\Srourcode\Srourcode\main.py", line 245, in <module>
    shipmentsdata = pd.read_csv('../input/sample-data-shipments-vehicle-routing-simulation/sampledatatruckrouting2.csv')
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\util\_decorators.py", line 211, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\io\parsers\readers.py", line 950, in read_csv
    return _read(filepath_or_buffer, kwds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\io\parsers\readers.py", line 605, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\io\parsers\readers.py", line 1442, in __init__
    self._engine = self._make_engine(f, self.engine)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\io\parsers\readers.py", line 1735, in _make_engine
    self.handles = get_handle(
                   ^^^^^^^^^^^
  File "C:\Users\lolaX\PycharmProjects\Srourcode\venv\Lib\site-packages\pandas\io\common.py", line 856, in get_handle
    handle = open(
             ^^^^^
**FileNotFoundError: [Errno 2] No such file or directory: '../input/sample-data-shipments-vehicle-routing-simulation/sampledatatruckrouting2.csv'
**
Process finished with exit code 1

I just do not know why the csv file is not seen as the directory. Here is how I read it:

# read csv
truck_data = pd.read_csv(r'C:\Users\lolaX\Desktop\sampledatatruckrouting2.csv')
print(truck_data)

It is reading and outputting the data but I do not know how to fix the error. Please help.

I tried using os and so many other things to fix the error and it did not work.

Lily Nader
  • 11
  • 3

1 Answers1

1

I guess you have used forward slash / instead of backslash \ in the path. If so use backslash for defining path. Refer this for more info: Forward and backslashes in file paths

Also make sure the path is correct as I can see two different paths for same file in the error message and the sample source code in the question.

I have tried to answer based on speculation of given information so my answer may not be useful, however I am open to feedbacks.

Elixir0101
  • 331
  • 1
  • 2
  • 15
  • Did your error got resolved? – Elixir0101 Nov 08 '22 at 16:09
  • Hi, well my error did not get resolved. I've been trying to run the script one chunk at a time, to see where the error is exactly. If there are two different paths for the same file is it fine or should I fix it? and how to fix it? When I ran each part alone it worked untill I got a name error: "NameError: name 'locations_list' is not defined" and this is the code from the script where I first see this name: def CW_truck_assignment(locations_list, weights_list, vr_status): – Lily Nader Nov 08 '22 at 19:08
  • If there are 2 different paths mentioned for the same file, you should definitely replace them with the correct file path which will be same in both case, you can just type the correct path in place of the wrong path in the code. This may help out as far as I know. However there is also a name error – Elixir0101 Nov 08 '22 at 23:59
  • As it says NameError is when a variable, function or module isn't valid or exists. You can make sure there is no misspelling in the argument name and it is defined inside the function. If that doesnt work handle the error using try and except statement. – Elixir0101 Nov 09 '22 at 00:02
  • Did the problem get resolved now? – Elixir0101 Nov 09 '22 at 08:10
  • @LilyNader you can accept the answer(the green tick) to let me know if the answer was useful in resolving the error or not. – Elixir0101 Nov 12 '22 at 10:03