Why can I suddenly no longer write to files in Python? - python

Whenever I try to write or modify the data of a file in any way I get this error everytime:
OSError: [Errno 9] Bad file descriptor
Here's what I've been trying to do:
# output.txt is the file already created inside of the same directory
with open(__file__.rsplit("\\", 1)[0] + "\\output.txt", "w") as f:
f.write("this should write to 'output.txt'")
I would try to dump with json or append with normal data but I'm continuously recieving the same error.
In the above example, here is the entire output of the terminal after execution:
C:\Users\USER\Documents\Programming\Code\Python\Testing>c:\Users\USER\Documents\Programming\Code\Python\Testing\z_3.py
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\Documents\Programming\Code\Python\Testing\z_3.py", line 4, in <module>
f.write("this should write to 'output.txt'")
OSError: [Errno 9] Bad file descriptor
This is all very strange because I've been able to write to files normally but I've just recently reinstalled windows and now it won't work.

Related

While opening the file getting OSError: [Errno 16] Device or resource busy: '/sys/kernel/debug/tracing/uprobe_events' error

Below python script is used to write data in to file
fileHandler = open("/sys/kernel/debug/tracing/uprobe_events", "w")
fileHandler.write("1")
fileHandler.close()
Error Message:
Traceback (most recent call last):
File "File_Operation.py", line 4, in <module>
fileHandler = open("/sys/kernel/debug/tracing/uprobe_events", "w")
OSError: [Errno 16] Device or resource busy: '/sys/kernel/debug/tracing/uprobe_events'
I got the same error. Unfortunately, it is a general error and without a context it is difficult to understand why it happened.
I've looked for solutions, but nothing very useful. You can have a look here, maybe your problem is related to another in this thread:
https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/what-is-error-ioerror-%5Berrno-16%5D-device-or-resource-busy-4175599788/

Error message when using openpyxl.load_workbook() function

I have just started to learn programming and I am currently trying to read an excel file from IDLE. I'm following instruction from the book "Automate the Boring Stuff". I have successfully imported openpyxl, and thereafter, as instructed tried wb = openpyxl.load_workbook('example.xlsx') where I exchanged "example" to the actual name of the workbook. However, I get this error message:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 117, in load_workbook
archive = ZipFile(filename, 'r', ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1216, in __init__
self.fp = io.open(file, filemode) FileNotFoundError: [Errno 2] No such file or directory: 'jan.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
wb = openpyxl.load_workbook('jan.xlsx')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 145, in load_workbook
raise InvalidFileException(unicode(e))
openpyxl.exceptions.InvalidFileException: [Errno 2] No such file or directory: 'jan.xlsx'
I don't understand how to solve this.
This error message simply says that python can't locate the file. When you try to open a file 'jan.xlsx', it's trying to locate it in the base directory of your code in your IDE. So say your code is in a directory called /Users/username/PycharmProjects/myCode
(I'm assuming here you are on a Mac OS as the path to your python suggests...
but jan.xlsx is in /Users/username
Since that is 2 directories up from your code directory, you can do one of two things:
Write in the absolute path to the file:
wb = openpyxl.load_workbook('/Users/username/jan.xlsx')
Use a relative path that is relative to the base project directory. Two dots in a relative path means one level up from the current directory. So if the excel file is 2 levels up, you can do:
wb = openpyxl.load_workbook('../../jan.xlsx')

OSError: [Errno 9] Bad file descriptor Showing while trying to export csv in python

I want to save or export a csv file in python with some data. But it's every time showing this error, even the code has no error I think.
Please help me out from this problem.
import csv
myFile = open('products.csv', 'a')
with myFile:
writer = csv.writer(myFile)
writer.writerow(['France', 'Paris'])
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "my_first_webscrap.py", line 32, in <module>
writer.writerow(['France', 'Paris'])
OSError: [Errno 9] Bad file descriptor

OSError: [Errno 22] Invalid argument:

I'm scraping a lot of reviews from a site with Python, for each review I call the "review" function and then open the file and append it to it. It works for a while but then the following error appears me everytime and not it the same review.
OSError: [Errno 22] Invalid argument
I tried json.dumps:
scraped_data = reviews(line)
with open('reviews','a' ) as f:
f.write(json.dumps(scraped_data,f,indent = 4))
but the same error keeps appearing. I also tried json.dump:
scraped_data = reviews(line)
with open('reviews','a' ) as f:
json.dump(scraped_data,f,indent = 4))
and, for some reason, I tried without indent too.
edit: full traceback for json.dumps:
Traceback (most recent call last):
File "s.py", line 202, in <module>
with open('reviews','a' ) as f:
OSError: [Errno 22] Invalid argument: 'reviews'
full traceback for json.dump:
Traceback (most recent call last):
File "s.py", line 203, in <module>
json.dump(scraped_data,f,indent = 4)
OSError: [Errno 22] Invalid argument: 'reviews'
On Windows 10
I noticed the same behavior in my code and I found that I was using Microsoft OneDrive which was causing the same error. The file I was trying to open had its file pointer visible in Windows Explorer but not the contents. Are you using any cloud file sharing service?
(I right clicked the file, selected "Always Keep on this Device", ran the same code again and it worked).
Why don't you open your file as a variable?
f = open("reviews", "a")
f.write(json.dumps(scraped_data,f,indent = 4))
f.close()
try giving it the full path of the file.
make sure you have permission to write in that directory (whatever user the app is running under)
also, if the file does not already exist, it cannot append to it... instead of a try a+
plus sign means if it is not there then create it

can't delete a file after no space left on device

I'm writing a bunch of files on several hard disks. All my files don't fit on a single hard drive so I write those on next one if the first one is out of space. I catch the IOError 28 to figure this out.
My exact problem is that when I try to remove the last file written (incomplete file) to the first disk I get a new Exception that I don't fully understand. It seems that with-block can't close the file because there is no space left on a disk.
I'm on windows and disks are formatted to NTFS.
Could someone please help me.
# Here's a sample code
# I recommend first to fill a disk to almost full with a large dummy file.
# On windows you could create a dummy file with
# 'fsutil file createnew large.txt 1000067000000'
import os
import errno
fill = 'J:/fill.txt'
try:
with open(fill, 'wb') as f:
while True:
n = f.write(b"\0")
except IOError as e:
if e.errno == errno.ENOSPC:
os.remove(fill)
Here's the traceback:
Traceback (most recent call last):
File "nospacelef.py", line 8, in <module>
n = f.write(b"\0")
IOError: [Errno 28] No space left on device
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "nospacelef.py", line 8, in <module>
n = f.write(b"\0")
IOError: [Errno 28] No space left on device
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "nospacelef.py", line 11, in <module>
os.remove(fill)
WindowsError: [Error 32] The process cannot access the file because it is being used by another process: 'J:/fill.txt'
Answering to my own question.
I filed a bug to python [1][2]. It was already fixed in 3.3+. There is no fix for 3.2 that I used. I upgraded my python version so I'm not suffering this problem anymore.
[1] http://bugs.python.org/issue25202
[2] http://bugs.python.org/issue16597

Categories

Resources