0

i am having this issue since like 2 months, it didnt bother me at first but now that im trying to import a file with pd or even a normal txt file with open() it gives me this Exception:

  File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 642, in get_handle
    handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'marketing.xlsx'

if i use the full path it just runs normally. people would say "just use full path then" but this is a really bad solution when it comes to using this program on multiple devices with different paths or stuff like that so i hope you have any solutions. here is the code:

import pandas as pd 
df = pd.read_csv('marketing.xlsx')

image: vscode folder

edit: it has none to do with the code itself but more like windows or my pc

6
  • I'd try printing out os.getcwd() and os.listdir() to see if you're actually executing in the path you expect, and the file is present according to the interpreter. Commented Apr 9, 2021 at 16:07
  • What @SamMorgan said but... are you sure that's a CSV file? The .xlsx filename extension indicates that that's an Excel workbook. CSV (comma-separated-values) means a delimited text file. An Excel workbook is a different beast. Commented Apr 9, 2021 at 16:11
  • it is indeed and excel file but that dosent change anything because in my code i typed read_csv('marketing.xlsx') Commented Apr 9, 2021 at 16:13
  • and os.listdir() returns ['main.py', 'marketing .xlsx', 'requirements.txt', 'setup.py'] Commented Apr 9, 2021 at 16:15
  • 1
    The os.listdir output you posted has a space after marketing, is that a typo ? Commented Apr 9, 2021 at 16:18

2 Answers 2

1

FileNotFoundError means that the file path you've given to pandas point to an non existing file.

In your case this can be caused by:

  • you haven't put your file in the current working directory of your script
  • you have a typo in your file name

In both case, I would print the file path, and check the location using a file browser, you will find your mistake:

print(os.path.join(os.getcwd(), 'marketing.xlsx'))
Sign up to request clarification or add additional context in comments.

Comments

0

i see a spaces in the file name there. I tried on mac, and it works very well. ['main.py', 'marketing .xlsx', 'requirements.txt']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.