I was having issues writing to file, I've tried a lot of different methods of writing to file and none have worked. I was trying to figure out what was causing it so have used f.open, write, close to narrow down where the issue is coming from. I've narrowed it down to the pd.read_excel file here:
import glob
import os
# Open most recent file (probably to be changed)
list_of_files = glob.glob(r'C:\Users\gamo0\Downloads\*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
f = open("demofile1.txt", "a")
f.write("Now the file has more content!")
f.close()
df = pd.read_excel(latest_file,sheet_name = 'People in your team',header=8)
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
It successfully writes to demofile1.txt and closes it. But then fails on writing to demofile2.txt with the following error:
f = open("demofile2.txt", "a")
FileNotFoundError: [Errno 2] No such file or directory: 'demofile2.txt'
A lot of similar issues talk about using absolute paths rather than relative, but it doesnt appear to be an issue with that as it can write to demofile1.txt fine.
N.B. if I move both write to demofiles above the pd.read_excel line, it will successfully write to both.
Any help would be appreciated.
I think it may be to do with how you are trying to write to the file. Try changing your open and write lines from:
f = open("demofile1.txt", "a")
f.write("Now the file has more content!")
f.close()
to
with open("demofile1.txt", "a") as f:
f.write("Now the file has more content!")
Using context managers is good practice when working with files. From the documentation here:
Context managers allow you to allocate and release resources precisely when you want to.
Related
I am fairly new to Python and I am trying to learn how to read and write csv files.I am programming on my iPad using Pythonista and I’ve encountered a problem I cant seem to solve. I want to read a csv file of which I don’t know the directory because of the limited iOS File Management App. The csv file is in the same folder where my python file is.
I’ve found on google that I can find the absolute directory by using the following code:
import os
print(os.path.abspath("google_stock_data.csv"))
Which spits out:
/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/google_stock_data.csv
Alright now on to my problem:
import csv
path = "/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/google_stock_data.csv"
file = open(path, newline= '')
reader = csv.reader(file)
header = next(reader)
data = [row for row in reader]
print(header)
print(data[0])
The upper code gives me the error:
FileNotFoundError: [Errno 2] No such file or directory: '/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/google_stock_data.csv'
I know that the file exists and the directory should be correct since I’ve also tried finding it with pathlib and it turned out to be the same.
So what seems to cause the problem?
Try using the with open() (read) syntax. I have something very similar and this works for me. Your path is correct.
with open(path, 'r', encoding='utf-8') as reader:
reader = csv.DictReader(reader)
for row in reader:
# ...do stuff
The problem lies within how I named my file. The name of the csv file that I wanted to open was called "google_stock_data.csv". Note that this is the filename and does not contain its file suffix at the end (which would be ".csv").
If you want to use file = open(...) you have to also add the file suffix at the end of the filename.
This means that this is how it should look like in my case:
file = open('google_stock_data.csv.csv', newline= '')
Finding the absolute path with print(os.path.abspath("enter_file_name")) is not needed if you have the file in the folder where your code is.If you do, for whatever reason, dont forget to add the file suffix at the end.
As for how to output anything from the file both my code and
with open(path, 'r', encoding='utf-8') as reader:
reader = csv.DictReader(reader)
for row in reader:
# ...do stuff
from #Michael M. work perfectly fine if you add .csv at the end of where you declare path.
I am currently stuck with the following error:
IOError: [Errno 2] No such file or directory: '/home/pi/V1.9/Storage/Logs/Date_2018-08-02_12:51.txt'
My code to open the file is the following:
nameDir = os.path.join('/home/pi/V1.9/Storage/Logs', "Date_" + now.strftime("%Y-%m-%d_%H:%M") + ".txt")
f = open(nameDir, 'a')
I am trying to save a file to a certain path, which is /home/pi/V1.9/Storage/Logs. I am not sure why it can't find it, since I have already created the folder Logs in that space. The only thing being created is the text file. I am not sure if its suppose to join up like that, but I generally tried to follow the stages on this thread: Telling Python to save a .txt file to a certain directory on Windows and Mac
The problem seems to be here:
f = open(nameDir, 'a')
'a' stands for append, which means: the file should already exist, you get an error message because it doesn't. Use 'w' (write) instead, Python will create the file in that case.
If you are creating the file use the write mode w or use a+
f = open(nameDir, 'w')
f = open(nameDir, 'a+')
Use only a append if the file already exist.
Not really an answer to your question, but similar error. I had:
with open("/Users//jacobivanov/Desktop/NITL/Data Analysis/Completed Regressions/{0} Temperature Regression Parameters.txt".format(data_filename), mode = 'w+') as output:
Because data_filename was in reality a global file path, it concatenated and looked for a non-existent directory. If you are getting this error and are referring to the file path of an external file in the name of the generated file, check to verify it isn't doing this.
Might help someone.
I'm having some problems getting a file to memory map, and was hoping to resolve this issue. I've detailed the problem and shown my code below.
What I'm importing:
import os
import mmap
Now, for the code:
file = r'otest' # our file name
if os.path.isfile(file): # test to see if the file exists
os.remove(file) # if it does, delete it
f = open(file, 'wb') # Creates our empty file to write to
print(os.getcwd())
Here is where I encounter the problem with my code (I've included both, and have one commented out each time I run the program):
mfile = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)
#mfile = mmap.mmap(f.fileno(), 10**7, access=mmap.ACCESS_WRITE)
I'm encountering an error with either of the mfile lines.
For the mmap.mmap line with the 0 argument I get this error: ValueError: cannot mmap an empty file. If I instead use the 10**7 argument I get this error instead: PermissionError: [WinError 5] Access is denied
And to end it:
"""
Other stuff goes here
"""
f.close() # close out the file
The 'other stuff here' is just a place holder for a where I'm going to put more code to do things.
Just to add, I've found this thread which I thought may help, but both the ftruncate and os.truncate functions did not seem to help the issue at hand.
As that thread that you linked shows, mmap requires you to create the file first and then modify it.
So first, create an empty file doing something like:
f = open(FILENAME, "wb")
f.write(FILESIZE*b'\0')
f.close()
Then, you will be able to access the file and mapping it using:
f = open(FILENAME, "r+b")
mapf = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)
Notice the way the file is being open. Remember that you can flush your buffer by doing (more details here Usage of sys.stdout.flush() method):
sys.stdout.flush()
Let me know if you want me to go into details about any of this points.
Hello to divine people with knowledge about Python.
I'm currently studying Python 3.4 and have encountered a problem.
About the load function, even when I used this function with the correct txtfile name an error appears.
results = []
with open('text.txt') as inputfile:
for line in inputfile:
results.append(line.strip().split(','))
Traceback (most recent call last):
File "F:/CSSE1001/Assignments/SunMum/030415.py", line 2, in <module>
with open('stations.txt') as inputfile:
FileNotFoundError: [Errno 2] No such file or directory: 'text.txt'
Looks like Python cannot find where the text.txt is..
Can someone please tell me how to set a directory of a function? Thank you.
You are probably looking for os.chdir(path). Alternatively, you could use os.path.join(path, *paths) to dynamically build the absolute path to hand over to the open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) function. It could also be that your text.txt file is not in the same directory as your 030415.py file (F:/CSSE1001/Assignments/SunMum).
Update: There are several ways you can load an entire file.
If you want to read all of a file as text, you can write data = open('text.txt', 'rt').read()
If you want to get all of a file's binary data, you can use data = open('text.txt', 'rb').read()
If you want the lines of a file, a simple way to get them would be with lines = tuple(open('text.txt', 'rt'))
If you want the lines without the newlines, you could type lines = open('text.txt', 'rt').read().split('\n')
If you want to process the lines efficiently while avoiding newlines, you could use for line in (line.strip('\n') for line in open('text.txt', 'rt')): to start a for loop.
If you are working with comma-separated values, you probably should look at the documentation for the csv module available with Python.
This question already has answers here:
open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory'
(8 answers)
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 6 months ago.
I am very new to Python so please forgive the following basic code and problem, but I have been trying to figure out what is causing the error I am getting (I have even looked at similar threads on S.O.) but can't get past my issue.
Here is what I am trying to do:
loop through a folder of CSV files
search for a 'keyword' and delete all lines containing the 'keyword'
save output to a separate folder
Here is my code:
import os, fnmatch
import shutil
src_dir = "C:/temp/CSV"
target_dir = "C:/temp/output2"
keyword = "KEYWORD"
for f in os.listdir(src_dir):
os.path.join(src_dir, f)
with open(f):
for line in f:
if keyword not in line:
write(line)
shutil.copy2(os.path.join(src_dir, f), target_dir)
Here is the error I am getting:
IOError: [Errno 2] No such file or directory: 'POS_03217_20120309_153244.csv'
I have confirmed that the folder and file do exist. What is causing the IOError and how to I resolve it? Also, is there anything else wrong with my code that would prevent me from performing the entire task?
Hmm, there are a few things going wrong here.
for f in os.listdir(src_dir):
os.path.join(src_dir, f)
You're not storing the result of join. This should be something like:
for f in os.listdir(src_dir):
f = os.path.join(src_dir, f)
This open call is is the cause of your IOError. (Because without storing the result of the join above, f was still just 'file.csv', not 'src_dir/file.csv'.)
Also, the syntax:
with open(f):
is close, but the syntax isn't quite right. It should be with open(file_name) as file_object:. Then, you use to the file_object to perform read or write operations.
And finally:
write(line)
You told python what you wanted to write, but not where to write it. Write is a method on the file object. Try file_object.write(line).
Edit: You're also clobbering your input file. You probably want to open the output file and write lines to it as you're reading them in from the input file.
See: input / output in python.
Even though #Ignacio gave you a straightforward solution, I thought I might add an answer that gives you some more details about the issues with your code...
# You are not saving this result into a variable to reuse
os.path.join(src_dir, f)
# Should be
src_path = os.path.join(src_dir, f)
# you open the file but you dont again use a variable to reference
with open(f)
# should be
with open(src_path) as fh
# this is actually just looping over each character
# in each result of your os.listdir
for line in f
# you should loop over lines in the open file handle
for line in fh
# write? Is this a method you wrote because its not a python builtin function
write(line)
# write to the file
fh.write(line)
Um...
with open(os.path.join(src_dir, f)) as fin:
for line in fin:
Also, you never output to a new file.
I solved the problem like so:
src_dir = "C:\\temp\\CSV\\"
target_dir = "C:\\temp\\output2\\"
keyword = "KEYWORD"
for f in os.listdir(src_dir):
file_name = os.path.join(src_dir, f)
out_file = os.path.join(target_dir, f)
with open(file_name, "r+") as fi, open(out_file, "w") as fo:
for line in fi:
if keyword not in line:
fo.write(line)
I got this error and fixed by appending the directory path in the loop. script not in the same directory as the files. dr1 ="~/test" directory variable
fileop=open(dr1+"/"+fil,"r")