fd.seek() IOError: [Errno 22] Invalid argument - python

My Python Interpreter (v2.6.5) raises the above error in the following codepart:
fd = open("some_filename", "r")
fd.seek(-2, os.SEEK_END) #same happens if you exchange the second arg. w/ 2
data=fd.read(2);
last call is fd.seek()
Traceback (most recent call last):
File "bot.py", line 250, in <module>
fd.seek(iterator, os.SEEK_END);
IOError: [Errno 22] Invalid argument
The strange thing with this is that the exception occurs just when executing my entire code, not if only the specific part with the file opening.
At the runtime of this part of code, the opened file definitely exists, disk is not full, the variable "iterator" contains a correct value like in the first codeblock.
What could be my mistake?
Thanks in advance

From lseek(2):
EINVAL
whence is not one of SEEK_SET,
SEEK_CUR, SEEK_END; or the resulting
file offset would be negative, or
beyond the end of a seekable device.
So double-check the value of iterator.

Related

Why can I suddenly no longer write to files in 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.

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

File not found with Python's fileinput

Given the following code:
# Edit build number in test report
print(path) # TODO remove
html_report = fileinput.input(path, inplace=True)
for line in html_report:
print(line.replace('$BUILD_NUMBER',
args.number).rstrip())
html_report.close()
I get the following output:
/home/jenkins/workspace/reports/report201610261053.html
Traceback (most recent call last):
File "report_generator.py", line 58, in <module>
for line in html_report:
File "/usr/lib/python2.7/fileinput.py", line 252, in next
line = self.readline()
File "/usr/lib/python2.7/fileinput.py", line 321, in readline
os.rename(self._filename, self._backupfilename)
OSError: [Errno 2] No such file or directory
If I just use the command:
gedit /home/jenkins/workspace/reports/report201610261053.html
I can check that the file exists. In fact, if it didn't I would expect this error to be raised in the fileinput.input() line, not in the line loop.
Any idea of what's wrong?
What is your "path" value?
I think you should try to use absolute path
You can also check the user permissions to file.
I don't see anything wrong in the code that you have shown.
I can check that the file exists. In fact, if it didn't I would expect
this error to be raised in the fileinput.input() line, not in the line
loop.
The error is reported only when an attempt is made to open file and it happens in the for loop.
Is it possible that your code is running under a different user and doesn't see the file on that path as compared to you manually verifying file existence?

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

where is the call to encode the string or force the string to need to be encoded in this file?

I know this may seem rude or mean or unpolite, but I need some help to try to figure out why I cant call window.loadPvmFile("f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm") exactly like that as a string. Instead of doing that, it gives me a traceback error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 415, in <module>
window.loadPvmFile("f:\games\#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")
File "F:\Python Apps\pvmViewer_v1_1.py", line 392, in loadPvmFile
file1 = open(path, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename:
'f:\\games\\#DD.ATC3.Root\\common\\models\x07300\x07mu\\dummy.pvm'
Also notice, that in the traceback error, the file path is different. When I try a path that has no letters in it except for the drive letter and filename, it throws this error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 416, in <module>
loadPvmFile('f:\0\0\dummy.pvm')
File "F:\Python Apps\pvmViewer_v1_1.py", line 393, in loadPvmFile
file1 = open(path, "r")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
I have searched for the place that the encode function is called or where the argument is encoded and cant find it. Flat out, I am out of ideas, frustrated and I have nowhere else to go. The source code can be found here: PVM VIEWER
Also note that you will not be able to run this code and load a pvm file and that I am using portable python 2.7.3! Thanks for everyone's time and effort!
\a and \0 are escape sequences. Use r'' (or R'') around the string to mark it as a raw string.
window.loadPvmFile(r"f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")

Categories

Resources