Can't open files saved using windows long path workaround - python

I am saving matplotlib figures savefig(filename) from my python script using an absolute path, which is very long.
I had problems with max path length limitation on windows Errno2 no such file or directory, so I started appending \\?\ to the beginning of the path and it seemed to work.
However, some of my saved figures seem to be corrupted, as I try to open them with windows default image viewer and it just closes without showing the image. Some other figures saved by the exactly same procedure worked fine though.
I have noticed that the figures that do not open have the \\?\ appended to the location property in the windows explorer file property dialog:
The ones working does not have it:
EDIT:
I found new information: When the whole absolute path + filename has 260 characters or more (without the \\?\ in the beginning), then I get this corrupted behavior and can't open my file.
EDIT 2:
I tried creating a text file (with open(filename,'wb') instead of saving a matplotlib figure. I get the same \\?\ in the location propert and when I try to open it this is what I get:
So basically the \\?\ allows me to create a figure or textfile, but I cannot open it anyway.
EDIT 3 - well it turns out the files are not corrupted, as I can move them to a folder closer to root and they can be opened with no problems.
Any ideas how to solve this?

Related

Issue in Visual Studio Code editor. Why are files closing when I save them?

Why are files closing when I save them?
When ever I create a new file, add a couple lines of code to the file, and attempt to save my work, the file immediately closes, right when I initiate the save. Furthermore, the file that closes is not appearing in the "Open recent" file list that's located in the file menu.
Edit : The file immediately closes after I save as it, because when I click "run Python File", It open File Explorer tab
Does anyone know if this is a known issue, or if I can solve this problem by reconfiguring a 'VS Code' setting?
Use Directories to Save Files
It is better to open directories in VS Code, than it is to open a single file without a parent directory.
File > Open Folder or Ctrl+K Ctrl+O
The folder that opens, using the keybinding above, can be used to save all the files you want.
Using the Open Recent File-list:
After opening Code, the last project (or folder) will already be open. If not, just go to
If not, you should be able to just go to
File > Open Recent > "Folder/Project Name"
If you use Windows, with the right button on the folder you can click on "open with code", it will open the folder directly in Code
TIP: "You can turn on auto-save — using File > Auto Save — to save time"
It was happening to me too. In my case it turn out to be a PHP-CS-FIXER extension has some error which was the reason the file got closed after saving. Try to disable PHP-CS-FIXER extension or may be in your case it could be some other ext

Python: it can't delete a file if I previously opened it (and closed) in Windows File Explorer

I'm generating PDF using Weasyprint (Python 3.8). The pdf is stored in a specific folder. Every time after it's generated I click on it to see the preview in File explorer, or even open to review in Adobe Acrobat. After that, if I close it and move the pointer to another file (i.e. deselect it) my code can't remove it. If I don't touch the file in file explorer, my code can remove it without any problem.
I also tried os.remove function but the result is the same.
The code I'm using to remove the file:
try:
if os.path.exists(pdf):
os.unlink(pdf)
except Exception as ex:
log(ex)
The code doesn't throw an exception, it just does nothing and after that, the file can't be even open in the file explorer. It says "The file can't be previewed" and if I try to open in Acrobat it shows the error "Access denied".
UPD It turned out Windows Explorer locks pdf files if opened manually or even clicked them. Not sure how to stop it from locking.

Speed up the QFileDialog File Browser displaying folders/files in the default directory

Operating System: Win10, QT:PySide2
I implemented a programm in python, that aquires images from a camera. This image-aquisition sequence is triggered by pushing a button "Start Sequence". To determine a saving directory for the images I am using QFileDialog. The button "Start Sequence" therefore first opens up a filebrowser where the user can choose the folder, where it wants the sequence to be stored. Then the image aquisition is started. The code for the filebrowser looks like the following:
def get_dir(self, name) # name is only a string
start_dir = "D://data_someperson//"
dialog = QFileDialog()
options = dialog.Options()
options |= dialog.DontUseNativeDialog # i dont know what this is doing, but without it nothing works
dialog.setFileMode(QFileDialog.Directory)
filename = dialog.getExistingDirectory( None, name, start_dir, options = options)
return filename
As mentioned the code above opens a filebrowser in the "start_dir" location. So the filebrowser would start up in "D://data_someperson//" from where the user can create a new folder or choose an existing one. This is then returned as "filename", where the image aquisition code stores the camera pictures. Now this works under the condition that the start_dir is equal to the working directory of my program. E.g. I run my main program via VScode in "D://data_otto" and start_dir is set to "D://data_otto". Here the file browser displays instantly (as it pops up) all existing files in "D://data_otto" and the user can easily create new folders without any problems.
My Problem now is the following: If "start_dir" is not equal to "D://data_otto" (eg, it start_dir is equal to "D://data_peter") the file browser pops up as usual in "start_dir" but basicly tries to "load" every existing file on my machine before it displays the files in "start_dir", where the user is supposed to choose a saving directory. This is also true when the user creates a new folder in "start_dir". If the user creates a new folder and confirms it in the gui, the function "getExistingDirectory" still waits for all files to be loaded, sort of in the background.
This delays my data aquisition by up to a minute. "D://" is a Hard Drive DT01ACA100 1TB and ~800Gb are occupied. I think my problem could be solved by using a faster drive, but there must be a way to restrict the Filebrowser's scope of files it loads to display, but i cant find such a workaround in the documentation... I observed, that the folders and files in start_dir are cached once they were loaded for a short amount of time. Extending this time would help aswell...
Ok it appears as I solved my problem by disconnecting two unaccessible "Network locations" that appeared under "This PC" as the drives "Y:" and "Z:". They where unaccessible (indicated as a Red cross in the Explorer), since the PC was disconnected from its local network and was moved to a different one. So just hit right click on them and choose "Disconnect" in the drop down menu to remove these "Network Locations". After that I had no more problems with the File Browser and everything needed is displayed instantly. No more delays/loading/buffering. THANKS goes to https://stackoverflow.com/users/2001654/musicamante.

Trying to install IDLE2HTML on Windows 10, how can I append .txt file to .def file using Python?

The read me says:
Copy IDLE2HTML.py to your the idlelib folder of your python
installation. Append the contents of 'config-extensions.txt' to your
'config-extensions.def' file (also in the idlelib folder) and restart
IDLE.
For more information on extending IDLE read 'config-main.def',
'config-extensions.def' and 'extend.txt'
Here is the code I am supposed to append from the txt file to the def file:
[IDLE2HTML]
enable=1
[IDLE2HTML_bindings]
idle2html=
My attempts to search for an answer led me to this post that had this code:
with open("test.txt", "a") as myfile:
myfile.write("appended text")
but I had a few questions:
Would that code work on a .def file or would only work for a .txt file
The text I need to append is 4 lines, would I just have to run it 4 times using each of the four times as an input?
Is there a way to append the .txt file directly on to the .def file?
Thanks in advance to any help I can get, I realize this is probably a very noob-y question but I am learning Python by following an online class and I have yet to learn how to work w/ reading and writing files.
You don't need to do this programmatically. What the installation instructions are actually saying is:
1.) Navigate to the folder where idle.py is saved. On Windows this should be at: C:\Python27\Lib\idlelib or something similar. If you're having a hard time finding it on Windows you can right click on it in the Start menu and navigate to the Shortcut tab - this should show you where it is located. If you're on another OS I'm sure a quick Google search can help you out.
2.) Copy the IDLE2HTML.py file that you downloaded to this folder
3.) Within this folder there should also be a file called config-extensions.def. You can open this with Notepad (or any other simple text editor - just use right-click and "Open with") and copy/paste those four lines onto the end of the file and save.
If all goes well you should see the new behavior once you restart IDLE.
If you can start IDLE, you can find the path to its directory as follows: on the top menu, click File, then Open Module. (The shortcut is Alt-M at least on Windows.) Enter idlelib. The title bar of the editor window will contain __init__.py - <path-to-idlelib>/__init__.py (x.y.z).

QImage does not load image even when it exists

def setPixData(self,data):
print(data) #this print the image file name correctly e.g asdfghj.jpg
img=QtGui.QImage(QtCore.QCoreApplication.applicationDirPath ()+"/temp/"+data)
pix=QtGui.QPixmap.fromImage(img)
self.driver_ui.pix.setPixmap(pix)
This loads the image correctly when I run it from the development code.
Once I freeze it with cx_freeze and install it in final destination directory, it does not load the image. Even though the image is in the specified path.
I have tried hardcoding the full path into the frozen version, still no luck.
I've placed a f.write() at every other line of the above code, and they all ran with no error.
I figured the problem is from QImage since it returns true on isNull()
What could be the problem, If QImage is not being added by cx_freeze, it will sure throw and exception and wont run the f.write() on next line.
I am lost here.
NOTE: it's a windows GUI app in python.
You are likely giving it an incorrect file path. The fact that the file exists somewhere doesn't help QImage, if you don't give it a correct file path.

Categories