Close a file from within python - python

I have a py script that runs every night collecting data from an api and updating a number of csv files (using pandas .to_csv. Sometimes one of the files might be open in excel (because I've been looking at it and forgot to close it). This raises a permission error PermissionError: [Errno 13] Permission denied:
I want to trap this error, close the csv file (or close excel) without saving, and then try writing to the file again.
Is there a way to do this from within python?

try to catch any IOError for write access, kill excel with windows taskkill command if exception found
import os
import time
try:
with open(r'C:\file.csv', 'a') as f:
pass
except IOError:
os.system('taskkill /F /IM excel.exe')
time.sleep(2)
# do your csv read write, for example
with open(r'C:\file.csv', 'w') as f:
f.write('data')

Probably you need to search if swap file(current opened file) exist in the same directory or not .
If swap file exist then might need to delete that. before writing to that file
Read about it here
FYI: Delete Swap Files (Not programmatically though)
Correct me if I am wrong!

Related

How to write to a file which is open?

I am writing my script's output to an Excel file using openpyxl, which is working fine. However, if the Excel file is accidently open while Python is trying to write to it, it shows a PermissionError- which is expected. On this case, what I would have preferred is to write somehow rather than showing the error. Is there a way to do this?
You can't really solve this because the problem is not with openpyxl per-se, rather with the file you manually opened. If you would have opened it as a Read-Only file, you will not get that error. Of course, you can't (easily) solve that programmatically as openpyxl is not aware of the open instances of Excel you have running in the background.
Note that when opening the file as read-only, you will not see the saved changes from openpyxl.
One possible workaround, to avoid losing your changes you were about to save, is to allow the user to close that instance of Excel and try to save again. Something like:
while True:
try:
wb.save(path)
except PermissionError:
input(f"Please close the excel {path} and press Enter")
else:
print(f"Excel file saved successfully at - {path}")
break
Alternatively save to a temporary file and let the user merge later:
from pathlib import Path
try:
wb.save(path)
except PermissionError:
path = Path(path)
temp_path = path.with_name(f"{path.stem}_temp{path.suffix}")
wb.save(temp_path)
print(f"Excel file saved temporarily at - {temp_path}")
else:
print(f"Excel file saved successfully at - {path}")
Warning: these might cover-up other possible PermissionErrors, so use with care.

The file is used by another process error followed by unexpected behaviour

I'm trying to create a .txt file and move it to a specified folder using the code below, but I get PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\Emre\Desktop\testbot\asdf\testuser.txt'
this error which is followed by the script creating a txt file in both the directory the script is running and the directory I wanted shutil to move the txt file to. What should I do? Thanks in advance.
import shutil
file = open("{}.txt".format(source), "w")
file.write("username = {}\n".format(source))
file.write("user_points = 200\n")
file.close
shutil.move("C:\\Users\\Emre\\Desktop\\testbot\\asdf\\{}.txt".format(source), "C:\\Users\\Emre\\Desktop\\testbot\\asdf\\users")
self.bot.say(channel, "You have been successfully registered, {}!".format(source))
Your code says
file.close
when it should say
file.close()
Since you are just "mentioning" the close method rather than actually calling it, the file is not getting closed. And because it is still open, you will not be able to move it.
Note that the best practice for opening files is to use a context manager:
with open("{}.txt".format(source), "w") as file:
file.write("username = {}\n".format(source))
file.write("user_points = 200\n")
shutil.move( ...
Then the file will get automatically closed when you exit the with clause for any reason—so you don't need to worry about closing it explicitly, even if you want to return early or raise an exception.

Failing to open csv file

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()

Python [Errno 13] Permission denied:

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)

IOError: [Errno 13] Permission denied

I have this piece of code to create a .json file to store python data. When i run it in my server i get this error:
IOError: [Errno 13] Permission denied: 'juliodantas2015.json' at line with open(output_file, 'wb') as fp:
Python code:
fich_input='juliodantas2015.txt'
output_file= fich_input.strip('.txt')+'.json'
import json
with open(output_file, 'wb') as fp:
json.dump('yes', fp)
In command line i typed chmod 777 *.py but still not working. How can i fix this ?
I had a similar problem. I was attempting to have a file written every time a user visits a website.
The problem ended up being twofold.
1: the permissions were not set correctly
2: I attempted to use
f = open(r"newfile.txt","w+") (Wrong)
After changing the file to 777 (all users can read/write)
chmod 777 /var/www/path/to/file
and changing the path to an absolute path, my problem was solved
f = open(r"/var/www/path/to/file/newfile.txt","w+") (Right)
IOError: [Errno 13] Permission denied: 'juliodantas2015.json'
tells you everything you need to know: though you successfully made your python program executable with your chmod, python can't open that juliodantas2015.json' file for writing. You probably don't have the rights to create new files in the folder you're currently in.
I have a really stupid use case for why I got this error. Originally I was printing my data > file.txt
Then I changed my mind, and decided to use open("file.txt", "w") instead. But when I called python, I left > file.txt .....
I faced same issue this morning when I tried to write data to opened excel file note that you can not edit a file when it's open .
Please close the file and then it work normally

Categories