I have simple code:
from xlrd import open_workbook
open_workbook('Book1.xls')
No matter how I save the file (xls or xlsx) or how I change the path to the file (C:\Users\... or C:\Users...) I keep getting:
IOError: [Errno 2] No such file or directory: '(insert whatever is in place of Book1.xls)'
I don't know what I could be doing wrong. I'm not trying anything specific yet I just want it to not throw up an error.
You are not including whole path of the file. The path will be looks like:
file="C:\\USER\\TEST\\FILENAME"
or
file=r":\USER\TEST\FILENAME"
If you are using single slash then you need to use 'r' or else it will throw error.
you are not doing workbook("Book1.xls")
you are passing it a path (with directories) ... one of the folders in the path doesnt exist
this is probably because you do something like
directory="C:\Users\Blah\..."
when you need to do
directory = r"C:\Users\Blah\..."
other wise the backslashes serve to escape the next character
Related
Hi I have a beginner problem. So I wanted to access csv file with jupyter notebook and I am using python. I am opening the jupyter notebook on visual studio code. So here is my code
import pandas as pd
df3 = pd.read_csv("D:/medali.csv")
imax = df3["bronze"].idxmax()
df3[imax:imax+1]
The thing is I kept stuck with the error
FileNotFoundError: [Errno 2] No such file or directory: 'D:/medali.csv'
I assume it is due to pathway problem so I've put the .ipynb file with the .csv file in one folder but it does not work. How to solve the error?
The easiest thing to do, assuming you're on windows os, is to go to the file, right click, select "copy as file path", and then put that in the place of "D:/medali.csv". That should fix the issue, but you may find that you also have to set the file path string as a raw string to keep it from being messed up by the \ or / characters that windows uses. To do this, type a single "r" in front of the file path string, without the quotations. Just the character "r".
Another thought to try is that you may need to actually "Open" the file first, and then try to read from it. Given that you're in python, I would recommend the following syntax:
import pandas as pd
with open(r"filepath.csv", "r") as f:
df3 = pd.read_csv("D:/medali.csv")
imax = df3["bronze"].idxmax()
df3[imax:imax+1]
This is best practice because when you open the file with the "with" keyword, it will close after the block under it has executed automatically.
Iam coding in vs code with python3.
in the tutorial says that you should write
f=open("filename.txt")
and then run it and if you want to read it
f=open("filename.txt","r")
and I write a txt in my computre and I'm sure its exicts in my loptop but every time I run the code it gives me :(FileNotFoundError: [Errno 2] No such file or directory)
what should I do?
You can try to change "filename.txt" for the absolute path of that file. That means you should go to your file and copy the exact complete path from the storage through all directories until reaching it:
f=open("filename.txt","r")
f=open(r"C:\\Users\\your_user\\Desktop\\filename.txt","r") #Example of an absolute path, in this case the file is in Desktop but you have to copy the exact path you have
If that doesn't work, the file might be corrupted or defective, so you would have to try other solutions.
I am trying to read my excel file with pd.read_excel in python.
But ı am get this error message = FileNotFoundError: [Errno 2] No such file or directory
I put my excel file same place with my python file.
pic1 pic2
You can use full path this way to read the excel file. And also add r prefix before the file path so backslash will be treated as literal character
pd.read_excel(r"C:\Users\selman\PycharmProjects\selman_learning\bisiklet_fiyatlari.xlsx")
import pandas as pd
path = 'absolute_path/records.xlsx' #eg. C:\\Projects\\readexcel\\file\\records.xlsx
df = pd.read_excel(path)
Do the above
I think you need to put file object and not only the path of the file.
Try to use:
with open("<path to your file>", encoding = 'utf-8') as f:
pandas.read_excel(f)
I think you can modify the code by writing it in this way:
pd.read_excel("./<file name>")
Providing the absolute path to the .xlsx file worked for me. For situations where you cannot anticipate what the absolute path will be, try the following:
import os.path
pd.read_excel(io=os.path.abspath('path\\to\\excel_file.xlsx'))
'path\to\excel_file.xlsx' should be the relative path to the .xlsx from the project root.
Credit to this answer to a similar question.
I'm trying to rename some files that have Chinese characters. However, the following won't work:
import os
for filename in os.listdir(r"C:\Users\mas\Desktop\"):
if filename.startswith("你好"):
os.rename(filename, filename[7:])
it gives the error " The system cannot find the file specified: '你好 Hello.txt"
Do I need to change some settings or something here?
Based on the error message, it seems the file is not found
I came across similar problem, and I solved it by changing current working directory first
In your case
# Change working directory first
os.chdir("C:\Users\mas\Desktop")
# then do the loop
for filename in os.listdir(r"C:\Users\mas\Desktop\"):
...
I have tried to append a record on the next line in the file using the following code(please note that the file has been created already). But, it does not insert any records at all. The file remains empty.
with open(utmppath+'/'+tmpfile, "a") as myfile:
myfile.write(record+'\n')
myfile.close()
Any suggestion would be great. Thanks
Check additionally if you set your path correctly:
import os
path = utmppath+'/'+tmpfile
assert os.path.isfile(path), path
The assertion checks if the file exists and raises an AssertionError if you used a wrong path. Additionally the used path is included in the error message thanks to the variable
after the comma.
Additionally I recommend you to join files with the help of os.path.join and os.path.abspath. os.path.join concatenates path strings correctly for you and os.path.abspath creates an absolute path.
path = os.path.join(utmppath, tmpfile)
Let's say the wished file is in the same directory like your script and called your_output.txt - you can use this:
path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'your_output.txt'))
By the way, __file__ gives you the name of your script file.