I'm getting a mypy syntax error on line 36 in every single one of my files. If the file is shorter than 36 lines it just highlights the last line. The error is invariably the same:
invalid syntax mypy(error)
Apparently this issue may have happened before, but without a clear solution. Is this a known bug? How can I fix this?
Here is a clear example:
I found the reason: I have another file where there is a syntax mistake on line 36. Even though I closed that file, somehow mypy was still highlighting that same line in all of the files I had open. After I fixed that mistake in that one file, the mysterious syntax errors were gone in all the other files as well.
This seems like a bug to me, but I'm not sure if it's mypy or vscode (the IDE i'm using)? In any case my conclusion from this is: if you get inexplicable errors on the same line in all files, check for a mistake on that line even in closed files and fix it.
Related
I want to copy the content of a file 'from_path' to the end of another file 'to_path'. I wrote the code
fd_from = os.open(from_path, os.O_RDONLY)
fd_to = os.open(to_path, os.O_WRONLY | os.O_APPEND)
os.copy_file_range(fd_from, fd_to, os.path.getsize(from_path))
os.close(fd_from)
os.close(fd_to)
However, I get the following error
OSError: [Errno 9] Bad file descriptor
on the third line.
This (or something similar) was working fine, but now I can't avoid said error, even though (I believe) I haven't changed anything.
I looked around online and figured that this error usually happens because a file was not properly opened/close. However, that should not be the case here.
If we do, for example
fd_to = os.open(to_path, os.O_WRONLY | os.O_APPEND)
os.write(fd_to, b'something')
os.close(fd_to)
Everything works smoothly.
Also, if I write the exact same code as the problematic one, but without O_APPEND, everything works as well.
I am using Python 3.8.13, glibc 2.35 and linux kernel 5.15.0.
Note that efficiency is important in my case, thus many of the alternatives I've came across are undesirable.
Some of the alternatives that were found to be slower than this particular method are:
Using subprocess to launch the unix utility cat;
Iterating over the lines of the first file and appending them to the second.
While I had the implementation with copy_file_range working, I managed to find that this was around 2.6 times faster than cat and 14 times faster than iterating over the lines.
I've also read about shutil and other methods, but those don't seem to allow appending of the copied contents.
Can anyone explain the problem? Does this function not work with append mode? Or maybe there is a workaround?
Thank you in advance for your help!
C:\Users\sanji\PycharmProjects\pythonProject2\venv\Scripts\python.exe C:/Users/sanji/PycharmProjects/pythonProject2/file.py
Traceback (most recent call last):
File "C:\Users\sanji\PycharmProjects\pythonProject2\file.py", line 1, in
import tkinter
File "C:\Users\sanji\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1769
if self._name in self.master.children:
^
SyntaxError: invalid syntax
Process finished with exit code 1
In the __init__.py file for Tkinter, versions 3.9.2 through 3.9.5 inclusive have that statement appearing only on lines 2556 and 2581. See here, for example.
In the 3.9 variants before that, it's on various similar lines, around 2552 and 2577 give or take a couple of lines for small file changes.
It has never been anywhere near line 1769 in any of the 3.9 releases (including the release candidates).
So, given it's complaining about the file in the path containing ...Python\Python39\lib\..., I don't think we need to look at any other versions. I would say it's a safe bet that the file has become corrupted somehow.
You should check that file for validity and, if there's something wrong with it (probable), you may need to re-install Python to fix it. We can probably only speculate as to how it became corrupted (if indeed it did).
Maybe someone accidentally edited it or maybe you have a problematic disk. It's hard to say without seeing the entire file (or at least some twenty lines around the one the error's reported on).
Yeah I know this or similar questions have been posted to this forum but so far none of the answers is sufficient to solve my problem:
Here I have the following code:
with open(filename,'r',buffering=2000000) as f:
f.readline() # takes header away
for i, l in enumerate(f): # count the number of lines
print('Counting {}'.format(i),end='\r')
pass
What happens is the file is a 23Gbytes csv file. I get the following error:
File "programs\readbigfile.py", line 33, in <module>
for i, l in enumerate(f): # count the number of lines
PermissionError: [Errno 13] Permission denied
The error always happens at the line number 1374200. I checked the file with a text editor and there is nothing unusual at that line. This happened to me with the same file but a smaller version (a few less Gigabytes). Then suddenly it worked.
The file is not being used by any other process at all.
Any ideas of why this error occurs in the middle of the file?
PD. I am running this program on a computer with an Intel i5-6500 CPU/16Gb memory and a NVIDIA GeForce GTX 750 Ti card.
System is Windows 10. Python 3.7.6 x64/Anaconda
The file is on a local disk, no networking involved.
Whatever it is, I think your code is ok.
My ideas:
do you need this buffering? Did you try to remove it completely?
I see you're running on Windows. I don't know if that's important, but many weird issues happen on Windows
if you're trying to access it on a disk in the network (samba etc), maybe it's not fully synced?
are you sure nothing else tries to access this file in the meantime? excel?
did you try reading this file with csv.reader? I don't think it'd help though, just wondering
you can try/except and when the error is raised check os.stat or os.access if you have permissions
maybe printing is at fault, it sounds like a huge file. Did you try without printing? You might want to add if i % 1000 == 0: print(...)
I found out the error is due to a file writing error, either because a bad disk block or a disk system failure. The point is, the file had a CRC error somewhere in the middle of it, which I corrected just by creating the file again. It is a random error so if you find yourself in the same situation, one of the checks should be the soundness of the file itself.
When I'm using Ctrl+Shift+I shortcut it fixes all errors like invalid count of blank lines. But E501 (too long line) is not fixing. Line saves its length. I've found solutions for ignoring that error but I'd like to translate file to PEP8 fully. How to do it?
I have a simulation in python which I have run, but half way to the end I got an error. I have already fixed the error. Now I want to execute the same file, but beginning in the line of the error. How can I do that? Execfile, as far as I looked doesn't do that...
You don't.
The easiest solution would be to comment out all the intervening lines, or put them inside an if False: block.
You could also simply save the appropriate portion of the code into a new file and run that instead.
Any of these operations should be trivial in most editors.
put the line in a class then just call that class. This is what I would do rather than commenting out the lines..