Writing to a file errors and permission errors - python

while True:
try:
enterName = input("Enter the name of the file:") + ".txt"
latinFile = open(enterName,"r")
read = latinFile.readlines()
for lines in read:
store.append(lines.strip())
checkSquare (store)
print ("File:")
for content in store:
print (content)
saveData(store)
I think the problem is with the code for saving the file contents.
The aim is for the code to open the file,read the contents and it checks to see if the format of the file is correct etc and then if that is all true and it works, it will save the file again (saveData) but it will rename the file so that it says in the directory that it has been validated.
However, the code isn't working ( the os.rename part) and also I keep getting a permissionerror and I don't know how to fix it.
Error message:
Traceback (most recent call last):
File "C:\Users\---\Desktop\python\idkll.py", line 44,in <module>
saveData(store)
File "C:\Users\---\Desktop\python\idkll.py", line 18, in saveData
os.rename (fileName,"VALIDATED" + fileName)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'OPENEDve.txt'

You should close your files as soon as you finish reading from them or writing to them. And you should close the file before attempting to rename it. You can either use the file's .close() method, or use a with statement so that files get closed automatically.
Incidentally, your saveData() function should probably not prompt the user for the filename: just pass it the name you already have in enterName. Also, prepending "VALIDATED" to the filename is not a good strategy. It's ok if you're just using relative filenames in the current directory, but it will make a mess of a proper file path.
In response to the update:
saveFile does not contain a file name string, it's a Python object representing your open file (also known as a file handle). The name of that file is the string in enterName. So you need to do something like
os.rename(enterName, enterName + "VALIDATED")

Just swap os.rename (fileName,"VALIDATED" + fileName) and file.close() lines inside saveData() function.
You should close the file prior to trying to rename it. The error is because your program is actually using the file it is trying to rename.

Related

Does try close the file after if it is successful?

I am trying to implement a quick code that checks to see if an Excel file is open. When the file is open, the IOError works and tells me the file is open. I can close and reopen the file at this point. If I do not have the file open, and either by calling a subprocess.call() or simply clicking on it, I an error.
Why does the try corrupt it if there is no error raises?
I have tried to close the file after the try but this does not work either.
file_path = (r'C:\users\Desktop\Build-Temp.xlsx')
if os.path.exists(file_path) is True:
report_closed = True
try:
report_opcl = open(file_path, 'w+')
# report_opcl.close()
except IOError:
print("file open already")
report_closed = False
# if report_closed is True:
# with report_opcl:
# subprocess.call(file_path,shell=True)
Errors
Excel cannot open the file 'Build-Temp.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.
Opening a file with 'w' will delete the file contents. The docs call this "truncating". You might avoid the corruption using 'a' instead (not tested).
Anyway this is not going to work. By the time you check that report_closed is true another process might have opened the file.
I think you are trying to solve an unsolvable problem. There is no way to make sure that a file (identified by its path) will be available to another process in the future.

Why do I get Python IOError: [Errno 13] Permission denied on writing to a file?

I cannot figure out what I am doing wrong. I am running some tests and writing the results to a file. The portion of the code which writes to a file is the following (in a class called Tester):
#staticmethod
def printHeader(resultsFileName):
if not os.path.isfile(resultsFileName):
# The file does not exist, thus
# we need to print the header
# Opens the results file
with open(resultsFileName,"a") as file:
# Prints the header
file.write("A long header")
#staticmethod
def printResults(resultsFileName,otherArguments):
# Prints the header if it does not exist
Tester.printHeader(resultsFileName)
# Prints the results
with open(resultsFileName,"a") as file:
file.write(otherArguments)
Sometimes I get this error:
Traceback (most recent call last):
File "main.py", line 74, in <module>
File "tester.py", line 88, in methodOne
File "tester.py", line 441, in printResults
File "tester.py", line 428, in printHeader
IOError: [Errno 13] Permission denied: 'results.txt'
while other times it runs smoothly. I cannot figure out where the problem is. Any ideas?
NOTE1: I have rwxpermission on the directory where the file is written.
NOTE2: The error happens after several lines of results have already been written. Thus, it happens when the code is checking whether the header should be printed or not (but it is not supposed to print it, since the file exists).
UPDATE 1:
As suggested, I have changed my code to avoid opening and closing the file multiple times. Now it writes everything in one shot. This is the updated code:
#staticmethod
def printResults(resultsFileName,otherArguments):
# Prints the header if it does not exist
if not os.path.exists(resultsFileName):
# The file does not exist, thus
# we need to print the header
# Opens the results file
# HERE IS WHERE ERRNO 13 HAPPENS
# STRANGELY, THE FILE DOES EXIST
# AS SEVERAL LINES OF RESULTS
# HAVE ALREADY BEEN WRITTEN
with open(resultsFileName,"w") as file:
# Prints the header
file.write("A suitable header")
# Prints the results
file.write(otherArguments)
else:
# Prints the results
with open(resultsFileName,"a") as file:
file.write(otherArguments)
It seems that os.path.exists() at some point returns FALSEeven if the file does exist. Probably, there is something revoking me permission to write (perhaps the file is not properly closed after writing?).
The explanation of os.path.exists() says that:
On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
UPDATE 2
I have changed my code to the following, to avoid os.path.isfile():
# Opens the results file
with open(resultsFileName,"a") as file:
if file.tell() == 0:
# Prints the header
file.write("Header")
# Prints the results
file.write(otherArguments)
file.close()
else:
# Prints the results
file.write(otherArguments)
file.close()
Nevertheless, ERRNO 13 happens at with open(resultsFileName,"a") as file:.
I have rwpermissions both on the folder and on the file, on which several lines are written before the error happens. The OS is Linux.
Try closing the file result.txt before running the program (I'd its open).
Check the writing permissions.
Another cause might be writing into a .txt file in a directory without those permissions. Try running python from the specified directory.
os.path.isfile(path) returns True if path exists AND is a file AND you have (at least) read permissions on it. IOW, it returns False if path exists but is a directory and/or you don't have permissions on it. So your test is wrong from the start - os.path.exists() would be a better choice. But anyway: those operations are not atomic so the file could well be created between the moment where you test for it's existence and the moment where you try to open it, so the whole design is actually unsafe. Also, stating, opening and closing a file are all costly operations, so I suggest - if possible - that you rethink your whole design to open a file only once and only close it once when you're done.
You can check if the file is open with another program. If so, you can try closing them all.
Cheers!
Surely it will work

Python 'continue' interfering with 'try/except' or 'with'?

I have an old script doing its duty for years now, which suddenly failed, and I don't see how it can even get to the point where it fails, since I thought to have that case eliminated:
It should go through a list of files (content of a directory), and if the current file ends with .sum, it should check if there is a file with the same name minus the .sum ending (like file1.txt.sum and file1.txt).
If both files exist, it should make a checksum of file.txt` and compare it to the content of the checksum file.
Only if this check is successful, there will be further operations ending with both files getting deleted from the directory.
for file in filelist:
checksum_downloaded_file, checksum_from_checksumfile = '0', '1'
if file.endswith('.sum'):
# read checksum from checksum file
f_sum = open(SAVETO + file, 'rb')
with f_sum:
checksum_from_checksumfile = f_sum.readline().split(' ')[0].rstrip().lower()
# checksum() the file
try:
f = open(SAVETO + file[:-4], 'rb')
except:
continue
with f:
checksum_downloaded_file = hashlib.sha256(f.read()).hexdigest()
# compare delivered checksum with selfmade
if checksum_downloaded_file == checksum_from_checksumfile:
[...]
sftp.remove(file)
sftp.remove(file[:-4])
I thought the check if a file exitsts with the same name as the checksum file except the .sum ending would be done in a reliable way by the
try:
f = open(SAVETO + file[:-4], 'rb')
except:
continue
block. But today the script failed while trying to delete both files at the end of the script, because the file without .sum ending didn't exist. How can this happen? Doesn't continue do its job in that context?
EDIT:
Since it seems there is nothing logically wrong with the code, the error might lay outside (although I don't know how it could). Therefore further informations: The actual source of the files is a directory reached via SFTP (pysftp used). The filelist is filled by filelist = sftp.listdir(), and all files are downloaded (to SAVETO) before the problematic block starts. They should get removed on the SFTP server with sftp.remove(file[:-4]) afterwards and this is where the script fails throwing
Traceback (most recent call last):
File "script.py", line 71, in <module>
sftp.remove(file[:-4])
File "/home/alba/.local/lib/python2.7/site-packages/pysftp/__init__.py", line 728, in remove
self._sftp.remove(remotefile)
File "/home/alba/.local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 365, in remove
self._request(CMD_REMOVE, path)
File "/home/alba/.local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 780, in _request
return self._read_response(num)
File "/home/alba/.local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 832, in _read_response
self._convert_status(msg)
File "/home/alba/.local/lib/python2.7/site-packages/paramiko/sftp_client.py", line 861, in _convert_status
raise IOError(errno.ENOENT, text)
IOError: [Errno 2] No such file or directory
EDIT2: Still can't let this go. I guess if the file got deleted on the server after the script downloaded it, the script would fail like this because of course the file wouldn't be there then anymore. But the script failed every time from then on. Doesn't make sense..
EDIT3: Solved in answer. Thank you for your time and patience.
My first approach to search for the bug in the code gave the title to this thread, and it turned out to be wrong.
The bug happens when the input directory gets fed with a checksum file, which already has been fed in the past, and back then correctly, ergo with a correspondent data file, but now without that correspondent data file:
The (again) downloaded checksum file gets evaluated and the script looks for a correspondent data file, which is still present in the working directory from the past. It runs its operations and finally tries to remove the checksum and the data file from the input directory, which fails for the data file.
An unforeseen, unhandled case; out of the initial specifications, but still embarrassing. I learned from this to sort the list of debugging approaches more realistically.

python 2.7: Testing text file in idle

I m using the command testFile = open("test.txt") to open a simple text file and received the following: Does such errors occur due to the version of python one uses?
IOError: [Errno 2] No such file or directory: 'Test.txt
add an "a+" mode argument on to your open, this will create the file if it doesn't exist:
testFile = open("test.txt", "a+")
Syntax for opening file is:
file object = open(file_name [, access_mode][, buffering])
As you have not mentioned access_mode(optional), default is 'Read'. But if file 'test.txt' does not exists in the folder where you are executing script, it will through an error as you got.
To correct it, either add access_mode as "a+" or give full file path e.g. C:\test.txt (assuming windows system)
The error is independent from the version, but it is not clear
what you want to do with your file.
If you want to read from it and you get such an error, it means that your file is not where you think it is. In any case you should write a line like testFile = open("test.txt","r").
If you want to create a new file and write in it, you will have a line like
testFile = open("test.txt","w"). Finally, if your file already exists and you want to add things on it, use testFile = open("test.txt","a") (after having moved the file at the correct place). If your file is not in the directory of the script, you will use commands to find your file and open it.

Opening '.txt' Files in Python

Answer Has Been Decided
The problem was not listing the entire file listing (i.e. DRIVE1\DRIVE2\...\fileName.txt).
Also, note that the backslashes must be turned into forwardslashes in order for
Python to read it (backslashes are outside of the unicode listing).
Thus, using: addy = 'C:/Users/Tanner/Desktop/Python/newfile.txt'
returns the desired results.
It's been a while since I have played with Python, and for my most recent class, we are required to make a BFS search that does a Word Puzzle that the Alice in Wonderland author created. I am just stating this, as the algorithm is the homework, which I have completed. In other words, my question does not apply to the answer to my homework question.
With that out of the way, I am in need of help on how to open, edit, read, create some form of text files in Python. My real problem is to place a list of words that I have inside of a .txt file, into a Dictionary dictionary. but I would much rather do this myself. Thus, I am left with how to do the said to text files.
NOTE:
I am running v3.3.
All documentation that I have found while searching how to solve this simple problem
is in regards to 2.7 or older.
I have tried to use:
>>> import sys from argv
>>> script, filename = argv
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
script, filename = argv
ValueError: need more than 1 value to unpack
I have also tried to use:
>>> f = open(newfile.txt, 'r')
But again, I get this error:
File "<pyshell#8>", line 1, in <module>
f = open (filename, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'newfile.txt'
However, I am positive that this file does exist. All of this being said, I am not sure if this is a directory problem, a problem understanding, or what... That is, anything would help!
First, if you want to retrieve a file name which is passed as the first argument to your script, use code like this:
import sys
if len(sys.argv) > 2:
filename = sys.argv[1]
else:
# set a default filename or print an error
Secondly, the error clearly indicates that the script can't find the file newfile.txt. So it is either not in the current directory, you don't have the permission to read it, etc...
To open a file for reading, use with command as follows
# This is python 3 code
with open('yourfile.txt', 'r') as f:
for line in f:
print(line)
with command used together with open command has already the raising exception process.

Categories