I have the following code:
for file in os.listdir('/home/sainik/Final/'+str(folderno)):
if file.endswith('.csv'):
print file
with open(file,'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print row
when running the code, I am getting the following error:
Traceback (most recent call last):
File "/home/sainik/Final/Programs/sainik.py", line 28, in <module>
with open(file,'rb') as csvfile:
IOError: [Errno 2] No such file or directory: '4.csv'
Kindly help.
You are trying to open the file from the path you are running the script.
You should try to open the full path
with open('/home/sainik/Final/' + file)
You are passing only the filename for open function. You should pass path to the open function. Two possible ways to pass the path of file to open function, either relative path or full path.
try:
with open( os.path.join('/home/sainik/Final/',str(folderno),file),'rb') as csvfile:
Your script is looking at it's own directory for file 4.csv.
Try it like this:
for file in os.listdir('/home/sainik/Final/'+str(folderno)):
if file.endswith('.csv'):
print file
with open(/home/sainik/Final/'+str(folderno)+'\/'+file,'rb') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
print row
Related
Working on a project, but I wanted read and write mode when I opened the csv file. Sadly, it is giving me an error. Here is the code:
import csv
with open("historicalData/ACAD.csv", "rw") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for lines in csv_reader:
print(lines[0])
And the error I am getting:
Traceback (most recent call last):
File "c:/Users/sande/Desktop/Vihaan/ThirdPartySoftware/Python/VisualStudiosCode/DadWork/test.py", line 3, in <module>
with open("historicalData/ACAD.csv", "ra") as csv_file:
ValueError: must have exactly one of create/read/write/append mode
Can anyone help in how to achieve this?
Use r+ or a+ or w+ not rw as python doesn't recognise it
This question already has answers here:
Python raising FileNotFoundError for file name returned by os.listdir
(3 answers)
Closed 3 years ago.
I am trying to read a CSV file. I have two CSVs under two folders. I get the CSV file but when I try to read the content, it says that I dont have such file or directory.
import os
import csv
def scanCSVFile(folder, output_file_name):
myfile = open(output_file_name, "a")
files = os.listdir(folder)
for file in files:
if file.endswith('.csv'): #gives two csv files
with open(file) as csvfile:
csvreader = csv.reader(csvfile)
next(csvreader)
for line in csvreader:
print (line)
myfile.close()
def openCSV(path):
scanCSVFile("./src/goi","goi.yml")
scanCSVFile("./src/kanji","kanji.yml")
openCSV('.')
Error:
C:\Users\Peace\Documents\LMS>python csvToYaml.py
Traceback (most recent call last):
File "csvToYaml.py", line 26, in <module> openCSV('.')
File "csvToYaml.py", line 24, in openCSV scanCSVFile("./src/goi","goi.yml")
File "csvToYaml.py", line 14, in scanCSVFile with open(file) as csvfile:
FileNotFoundError: [Errno 2] No such file or directory: 'Pro.csv'
You have to delete break from code
import os,glob
folder_path = r'path'
for filename in glob.glob(os.path.join(folder_path, '*.csv')):
with open(filename, 'r') as f:
for line in f:
print(line)
You can read data from csv file using pandas.Data will be stored in dataframes.
".yml" extension is obsolete now. Mostly ".yaml" is used.
The below link shows how to read .yaml files.
How can I parse a YAML file in Python
# loading libraries and reading the data from csv files.
import pandas as pd
market_df = pd.read_csv("./src/goi.csv")
print(market_df)
I'm getting a file not found error in JupyterLab. I can see the file and I have tried several different ways to open it via JupyterLab, but I keep getting an error as if the file does not exist.
Below is my code:
import os
import csv
csvpath = os.path.join('..','Resources','budget_data.csv')
with open(csvpath, newline="") as csvfile:
`enter code here`csvreader = csv.reader(csvfile, delimiter=",")
Here is the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-17-3c32e2cc9d43> in <module>
----> 1 with open(csvpath, newline="") as csvfile:
2 csvreader = csv.reader(csvfile, delimiter=",")
FileNotFoundError: [Errno 2] No such file or
directory: '..,Resources,budget_data.csv'
Here is the path: GTATL201902DATA3/03-Python/Homework/Instructions/PyBank/Resources/budget_data.csv
csvpath = os.path.join('..','Resources','budget_data.csv')
Are you absolutely sure this is the actual code you're using? Because going by the error message, it looks like you did this instead:
csvpath = os.path.join('..,Resources,budget_data.csv')
i.e. the .. and the commas are literally part of the string, instead of joining three separate strings.
I saved csv file in (C:/Users/kgu/PycharmProjects/untitled1/test1.py). named as testdata.csv
my code is :
import csv
f=open('testdata.csv','r',encoding='utf-8')
And the error message is here :
C:\Users\kgu\Anaconda3\python.exe
C:/Users/kgu/PycharmProjects/untitled1/test1.py
Traceback (most recent call last):
File "C:/Users/kgu/PycharmProjects/untitled1/test1.py", line 3, in <module>
f=open('testdata.csv','r',encoding='utf-8')
FileNotFoundError: [Errno 2] No such file or directory: 'testdata.csv'
Process finished with exit code 1
I did save the testdata.csv in the python project folder.
I don't know what is wrong in it.
If you use Pandas, you can try:
import pandas as pd
filepath = "/yourfilepath/yourfile.csv"
# check your sep (";" , "," ...)
f = pd.read_csv(filepath, encoding="utf-8", sep="yoursep")
You could do it like this - if you are in the right working directory:
with open(testdata.csv', 'rb') as csvfile:
data = [x for x in csv.reader(csvfile, delimiter='yourdelimiter')]
import csv
with open('database.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row['NAME'])
Guys, I have a csv file with the first row as a index, and in linux the code read from row['NAME'] and print only the names form colum NAME, when I run it in windows, it says:
C:\Users\Desktop>python py.py
Traceback (most recent call last):
File "py.py", line 5, in <module>
print(row['NAME'])
KeyError: 'NAME'
WHY?
If you are using the python 2- versions, you need to open the csv with rb, i.e.:
with open('database.csv', 'rb') as csvfile:....
for reference, checkout https://docs.python.org/2/library/csv.html as it includes a part in the reader doc about this.