I have the following log file called log.txt, with all the file names to be considered from a folder:
log.txt
C:\data\01.log
C:\data\02.log
C:\data\03.log
C:\data\04.log
My task is to read these files one after another from log.txt using a for loop.
with open("C:\data\log.txt",'r') as f:
logs=f.read()
print logs
for line in logs:
line = myfile.readline().replace('\n', '')
with open(line, 'r') as myfile:
lines = [line.rstrip('\n') for line in myfile.readlines()]
I am getting this error:
IOError: [Errno 2] No such file or directory:
What is the error you are getting?
Is it "IOError: [Errno 2] No such file or directory:"?
This error means that the directory C:\data\ does not exist. Are you sure this folder exists? Also if it does exist, is the logs.txt file in that directory?
I personally do not have a C:\data directory, so unless you created it, you have the address of the wrong directory.
Related
I am trying to reach to the directory path of my edited text files with no success. This is my script:
contents = ["The document is only available for reading",
"Summary of family root work",
"Japan Culture: Long life"]
filenames = ["doc.txt", "report.txt", "representation.txt"]
for content, filename in zip(contents, filenames):
file = open(f"../Files/{filename}", 'w')
file.write(content)
The code generates the following error:
Traceback (most recent call last):
File "C:\Python\App1- To do list\venv\Bonus\bonus5.py", line 8, in <module>
file = open(f"../Files/{filename}", 'w')
FileNotFoundError: [Errno 2] No such file or directory: '../Files/doc.txt'
Note, the code written in a python file under different directory than the expected name "Files". The directory "Files" is existing but python don't interprets it. I expect to see the modified content for each text file. Please guys, how can I fix this problem?
I'm trying to download a file but when it's trying to write to the current directory it gives a permission error
Traceback (most recent call last):
File "C:\Users\HP User\Desktop\WWE Tool\MasterDownload.py", line 22, in <module>
with open(x, 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\HP User\\Desktop\\WWE Tool'
Code:
MasterDownload = requests.get(url=Master, headers=Heads)
fpath = os.getcwd()
with open(fpath, 'wb') as f:
f.write(MasterDownload.content)
I checked the current path and eveything looks fine, I just can't get around as to why it's not writing as I am an admin
You're actually trying to write to a directory (the process' current working directory - as obtained from os.getcwd()), not to a file. Try selecting an actual file in that directory to write to instead of the directory itself, and the issue might go away.
I am trying to write to create and write to a text file. However, the error
Traceback (most recent call last):
File "/Users/muitprogram/PycharmProjects/untitled/histogramSet.py", line 207, in <module>
drinktrainfile = open(abs_file_path, "w")
IOError: [Errno 21] Is a directory: '/Users/muitprogram/PycharmProjects/untitled/HR/train.txt'
shows up. There are other instances of this in Stack Overflow, however, none of these deal with creating and writing a new file. The directories all exist however- only the file is being created The code that does this is:
import os
script_path = os.path.abspath(__file__) # i.e. /path/to/dir/foobar.py
script_dir = os.path.split(script_path)[0] # i.e. /path/to/dir/
rel_path = str(j) + "/train.txt" # HR/train.txt
abs_file_path = os.path.join(script_dir, rel_path) #/path/to/dir/HR/train.txt
drinktrainfile = open(abs_file_path, "w")
EDIT: train.txt shows up, except as a directory. How do I make it a text file?
The resource is actually a directory. It was very likely a mistake, as it is not likely that somebody would have created a directory by that name. First, remove that directory, and then try to create and open the file.
You can open the file with open('/Users/muitprogram/PycharmProjects/untitled/HR/train.txt', 'w'), assuming that the file does not already exist.
The code below is part of a program I am writing that runs a method on every .py, .sh. or .pl file in a directory and its folders.
for root, subs, files in os.walk("."):
for a in files:
if a.endswith('.py') or a.endswith('.sh') or a.endswith('.pl'):
scriptFile = open(a, 'r')
writer(writeFile, scriptFile)
scriptFile.close()
else:
continue
When writing the program, it worked in the directory tree I wrote it in, but when I moved it to another folder to try it there I get this error message:
Traceback (most recent call last):
File "versionTEST.py", line 75, in <module>
scriptFile = open(a, 'r')
IOError: [Errno 2] No such file or directory: 'enabledLogSources.sh'
I know something weird is going on because the file is most definitely there...
You'll need to prepend the root directory to your filename
scriptFile = open(root + '/' + a, 'r')
files contains only the file names, not the entire path. The path to the file can be obtained by joining the file name and the root:
scriptFile = open(os.path.join(root, a), "r")
You might want to have a look at
https://docs.python.org/2/library/os.html#os.walk
I have a directory /project/my_name/
I need to create a file /project/my_name
But :
with open(filename, 'w') as outfile:
outfile.write(my_content)
Raise IOError: [Errno 21] Is a directory: u'/project/my_name'
Any help?
From the filesystem's point of view, a directory is a kind of file. You can't have two files with the same name.