My current python script is like this:
import csv
with open ('2017_IL_sales.csv','r') as infile:
reader=csv.reader(infile)
with open('2017_IL_sales_report.csv') as outfile:
writer=csv.writer(outfile)
dict_report={rows[1]:rows[2] for rows in reader}
print dict_report
In brief, I want to open a csv file called 2017_IL_sales then create a dictionary for two columns inside. But with whatever reason, everytime I run the code via IDLE it told me this IOError: [Errno 2] No such file or directory: '2017_IL_sales.csv'. Anyone knows the reason?
Make sure that the script you are running is in the same folder as the file you are trying to read. If that is not possible, make sure to specify the correct file path.
Is IDLE's working directory that folder?
Run this in IDLE and see what you get:
import os
os.getcwd()
Related
I don't know what's wrong here, all I'm trying to do is to open this file, but it says it can't find such a file or directory, however as I have highlighted on the side, the file is right there. I just want to open it. I have opened files before but never encountered this. I must have missed something, I checked online, and seems like my syntax is correct, but I don't know.
I get the same error when I try with "alphabetical_words" which is just a text file.
When open() function receives a relative path, it looks for that file relative to the current working directory. In other words: relative to the current directory from where the script is run. This can be any arbitrary location.
I guess what you want to do is look for alphabetical.csv file relative to the script location. To do that use the following formula:
from pathlib import Path
# Get directory of this script
THIS_DIR = Path(__file__).absolute().parent
# Get path of CSV file relative to the directory of this script
CSV_PATH = THIS_DIR.joinpath("alphabetical.csv")
# Open the CSV file using a with-block
with CSV_PATH.open(encoding="utf-8") as csvfile:
pass # Do stuff with opened file
You need to import csv. Then you can open the file as a csv file. For example,
with open ("alphabetical.csv", "r") as csvfile:
Then you can use the file.
Make sure the file is in your cwd.
I am trying to read in a JSON file called league.json from a python file called run_test.py. The JSON file is in a different directory. I am running Windows 10 and python 3.8.6 in Visual Studio.
The directory I'm running the python file in is: C:\Users\my_un\Documents\folder1\folder2\model\run_test.py
The directory I am trying to load the json file from is:
C:\Users\my_un\Documents\folder1\folder2\config\league.json
Based on documentation I've found, the below code should work, but I keep getting the error: FileNotFoundError: [Errno 2] No such file or directory: 'config\league.json'.
This does run successfully when I move league.json to the same directory as the python file, and change the open location to: '.\league.json'.
What am I doing wrong? My code is below. Thanks in advance.
import json
with open('.\config\league.json', 'r') as f:
config = json.load(f)
print (len(config))
change the path to open('..\config\league.json', 'r') , by adding one more .
if the above doesn't work, use abspath to find the path of league.json and pass in the open(path) function
The folder 'config' needs to be in the folder 'model' for your code to run. Otherwise change your code to
open('C:\Users\my_un\Documents\folder1\folder2\config\league.json', 'r')
I'm attempting to write a quick python script to iterate through all csv files in the current folder and remove the header row from them then store them in a separate folder.
The current working directory has four sample csv files and the python script. Once executed the script creates the HeaderRemoved directory.
It appears that once the folder is created the code that is attempting to read the files is trying to access the folder but looking at the code I'm not sure why it would be.
I'm on a windows machine at the moment.
import csv, os, argparse, string
from ctypes import *
os.makedirs('HeaderRemoved', exist_ok=True)
# Loop through files in the current working directory
for csvFile in os.listdir('.'):
if not csvFile.endswith('.csv'):
continue # Skips non-csv files
print ('Removing header from ' + csvFile + '...')
# Read in CSV skipping the first row
csvRows = []
csvFileObj = open(csvFile)
csvReader = csv.reader(csvFileObj)
for row in csvReader:
if csvReader.line_num == 1:
continue # Skips the first row
csvRows.append(row)
csvFileObj.close()
# Write out the CSV file
csvFileObj = open (os.path.join('HeaderRemoved', csvFile), 'w', newline='')
for row in csvRows:
csvWriter.writerow(row)
csvFileObj.close()
Sample output:
Removing header from examplefile_1.csv...
Removing header from examplefile_2.csv...
Removing header from examplefile_3.csv...
Removing header from examplefile_4.csv...
Traceback (most recent call last): File "atbs_csv_parse.py", line 14, in <module>
csvFileObj = open(csvFile) PermissionError: [Errno 13] Permission denied: 'HeaderRemoved'
In my case, I had opened the csv file via Excel and ran the script. Then this Permission denied exception occurred.
Just closed the opened file and run the script again :)
In my case, the same error was because I was passing a directory name instead the file name.
Maybe could be the same problem of others.
The file in the script is opened somewhere in the system. That is the reason for getting "PermissionError : [Error 13]".
Solution:
Just close the file and run the script. You won't get the error.
As Charles Duffy commented under my original question, the issue was in fact that the lines of code for reading and writing the files had not been indented to fall within the for loop. Correcting the indentation fixed the issue and it now works as desired.
A good reminder to always check the simple things.... I got so wrapped up in why it wasn't working that I didn't even notice the lack of indentation.
If you are facing the problem where you can use the csv file with hard coded path but can't use with the windows directory or file path as the pandas or other library do not have the permission to use that object diectly, to so you have to convert it to stirng and use. I faced this issue and this solution worked for me.
from pathlib import Path
cur_dir=Path.cwd()
#cwd = os.getcwd()
csv_path=str(cur_dir)+"\\..\\Dataset\\FuelConsumptionCo2.csv"
df = pd.read_csv(csv_path)
I am new to python, and I am trying to open a video file "This is the file.mp4" and then read the bytes from that file. I know I should be using open(filename, "rb"), however I am not clear about the following things:
In what directory is python looking for my file when I use open()? (My file is located in the downloads folder, should I move it? Where?
Is using "rb" the correct way to read the bytes from a file?
So far I tried to open the file and I get this error:
IOError: [Errno 2] No such file or directory: 'This is the file.mp4'
I know it is probably an obvious thing to do, however I have looked all over the internet and I still haven't found an answer.
Thank you in advance!
By default, Python opens the file from the current working directory, which is usually the folder where the .py script of the program is located.
If you move the video file in the same directory as the script, it should work.
You can also view the current working directory like this:
import os
print os.getcwd()
Also, instead of moving the file, you can just change "This is the file.mp4" to "C:/Users/<username>/Downloads/This is the file.mp4" if you are using Windows 7 and maybe 8. You will have to change the <username> to your computer username.
Wildcards might also work: "~/Downloads/This is the file.mp4"
Finally, what are you planning to do with the video file bytes? If you want to copy the file to somewhere else, there are modules to do that.
"rb" is a correct way to read bytes of a file.
Currently I am trying to read in a csv file using the csv module in python. When I run the piece of code below I get an error that states the file does not exist. My first guess is that maybe, I have the file saved in the wrong place or I need to provide pyton with a file path. currently I have the file saved in C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv.
one side note im note sure wether having set the mode to 'rb' (read binary) was the right move.
import csv
with open('test_satdata.csv', 'rb') as csvfile:
satreader = csv.reader(csvfile, delimiter=' ', lineterminator=" ")
for row in satreader:
print ', '.join(row)
Here is the errror code.
Traceback (most recent call last):
File "C:/Python27/test code/test csv parse.py", line 2, in <module>
with open('test_satdata.csv', 'rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'test_satdata.csv'
Your code is using a relative path; python is looking in the current directory (whatever that may be) to load your file. What the current directory is depends on how you started your Python script and if you executed any code that may have changed the current working directory.
Use a full absolute path instead:
path = r'C:\Documents and Settings\eag29278\My Documents\python test code\test_satdata.csv'
with open(path, 'rb') as csvfile:
Using 'rb' is entirely correct, the csv module recommends you do so:
If csvfile is a file object, it must be opened with the ‘b’ flag on platforms where that makes a difference.
Windows is such a platform.
You can hit this error when you're running a Python script from a Directory where the file is not contained.
Sounds simple to fix, put the CSV file in the same folder as the .PY file. However when you're running under an IDE like VSCode the command output might cd to another directory when it executes your python file.
PS C:\git\awesome> cd 'c:\git\awesome'; ${env:PYTHONIOENCODING}='UTF-8'; ${env:PYTHONUNBUFFERED}='1';
& 'C:\Program Files (x86)\Python37-32\python.exe' 'c:\Users\jeremy\.vscode\extensions\ms-python.python-2019.9.34911\pythonFiles\ptvsd_launcher.py'
'--default' '--client' '--host' 'localhost' '--port' '1089'
'c:\git\Project\ReadCSV.py'
See how I'm in my awesome repo and the first command is: cd 'c:\git\awesome';
Then it executes the python file: 'c:\git\Project\ReadCSV.py'
So its expecting the CSV file in 'c:\git\awesome'.
To fix it, either use the full file names or CD to the directory containing the CSV file you wish to read.
Your current guess is right: either put the file in your test code directory or point python to the right path.
Make a fresh rename of your folder. That worked for me.