different behavior of shutil.move on Windows and Mac - shutil

Trying to move files from one directory to another. On a Mac, the following work fine. On a PC I get an error when the last file is reached saying it can't be moved because it is in use by another application or process. However, the file is moved (or a copy of is made) AND the original stays in the same location.
import sys, os
import shutil
sourcepath="D:\Data\MP-PO-to-QB\NEWXMLS"
for filename in os.listdir(sourcepath):
current_file=os.path.join(sourcepath, filename)
dst="D:\Data\MP-PO-to-QB\CONVERTED"
shutil.move(current_file, dst)

On Windows, if you open a file before moving it, you will get an error message like the following:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:
You should close the file before moving it.

Related

Export from Insomnia on Linux not working

when I attempt to export data (I want to export all my requests and import them on a different PC) as per the manual (https://docs.insomnia.rest/insomnia/import-export-data), I keep failing to achieve that. I suppose it should store the export on my hard drive (Linux, so in my Home directory), but when I run the export, I only see an empty folder: Empty Home folder.
When I try to store the exported data again, I already can see the previous stored export. However, when I attempt to locate the file on my hard drive using find command, it comes up empty. It appears as if Insomnia is storing the export on some kind of a virtual drive that I can't actually access. I couldn't find anything about this issue online, the few articles related to Insomnia export implicitly suggest that the export gets automatically stored on the real hard drive. Unfortunately, that is not my case. Also, when I open the import dialog on the target PC, it also opens an empty Home folder, so the problem is not restricted just to one PC.
Please, how do I get the export to work with my normal hard drive? Thanks a lot in advance!
https://github.com/flathub/rest.insomnia.Insomnia/issues/4
"It seems like the flatpak exec command should include the additional argument --filesystem=home.
A temporary workaround is to edit the file at /var/lib/flatpak/[...]/rest.insomnia.Insomnia.desktop and add the argument to the Exec section."
[Desktop Entry]
Name=Insomnia
Exec=/usr/bin/flatpak run --branch=stable --arch=x86_64 --filesystem=home --command=/app/bin/insomnia --file-forwarding rest.insomnia.Insomnia --no-sandbox ##u %U ##
...
Note I added the --filesystem=home argument to the entry file."
Then restart the PC

Python 3.7 app seems to stop connecting to internet when I use cx_Freeze to turn the app into an executable

I'm trying to create a python app that will check for newer versions of itself on GitHub. The way it checks is by finding a txt file in the repository that has the latest version number on it. My code works just fine and does what I want it to when running it with PyCharm but when I use cx_Freeze and run the exe it has trouble.
I started by using requests which worked fine until freezing, then I switched to urllib3 which had the same result. I tried simply pinging google which worked after freezing so I know it's not an issue with connecting to the internet.
from urllib3 import PoolManager
version_url = "https://raw.githubusercontent.com/redscientific/CompanionApp/master/Version.txt"
def get_data():
mgr = PoolManager()
r = mgr.request("GET", version_url)
...then I parse the data etc.
Before I freeze it I get the results I need but after I freeze it it seems to have trouble at r = mgr.request("GET", version_url)
I don't know what error it's having because I don't know how to get errors back from a .exe other than printing lines to a file but it won't get to any lines after mgr.request() so I can't output anything after the error happens.
I guess my question is how do I get it to work after I freeze it?
The error it spits out is as follows:
urllib3.exceptions.SSLError: Can't connect to HTTPS URL because the SSL module is not available.
Apparently cx_Freeze does not automatically add a couple of required .dll files. A fix I have found upon more googling is described here.
Try to run your exe from a cmd prompt or terminal, you should be able to see the error message in the terminal window then.
If you still prefer to print the exception to a file from within the application, you can use a try ... except block to catch the exception and use then traceback.print_exc() to print the exception, see How to print the full traceback without halting the program?
Assuming the line r = mgr.request("GET", version_url) causes the exception, replace it by:
import traceback
try:
r = mgr.request("GET", version_url)
except Exception:
with open('logfile.txt', 'w') as f:
traceback.print_exc(file=f)

IDLE Error: Opens my .py file instead of the .txt data file

I'm a Python/programming novice and this is probably an novice mistake, but I've scoured the internet for answers and have found none! I'm learning on a Rosalind module that's about opening data files. http://rosalind.info/problems/ini5/ I'm pretty sure I understand everything clearly, so I'm frustrated about my inability to do this simple task.
I'm using Python 3.6.2 and IDLE. The assignment is to simply open a .txt file and read a few lines.
I downloaded the .txt file to my working directory. Then, I opened up IDLE Shell and made sure I was in the right working directory (using ls & cd). I then opened a new IDLE .py file and wrote a script:
f = open('filename.txt', 'r')
f.readlines()[2]
I saved the script as p5.py. Then, I tried to run the script by calling F5. In the Shell, I got this message:
================ RESTART: /Users/liv/Desktop/Rosalind/p5.py =================
Is that an error? I think it's just a message from IDLE that IDLE has opened p5.py. Therein lies my problem, because now I have the wrong file open.
I started realizing that when I used the Shell and called it to print, and it came back with an empty string.
What am I doing wrong?? How do I get IDLE to open the filename.txt file? ...not the .py file.
The RESTART line means that IDLE has sent p5.py to Python to be run, which is exactly what you want and what you asked. Python should have then opened the text file, read it, retrieved the 3rd line, and stopped. Since p5.py has no output statements and does not raise any exceptions, you will not see anything. If you change the 2nd line to
print('line is ', f.readlines()[2])
then you should see something.

Perl Win32 - Geting file handle provided file and removing it

I have a file (ex. C:\temp\afile.txt) in which a Windows service has an open file handle on. After stopping the process, the file handle remains open. I would like to be able to find and delete this handle simply provided the file name and path with a Perl script. Is this possible? Thank you for your time.
It is possible to locate which process hold a file handle open and to reach into the process and kill the handle because MS's Process Explorer can do just this. How? I don't know.
You should probably use MoveFileEx(file_name, NULL, MOVEFILE_DELAY_UNTIL_REBOOT) instead. This causes the file to be deleted the next time the system is rebooted.
Win32API::File provides a Perl interface to that system call.

copy command syntax error in Windows XP

Basically I am just trying to create a batch file to copy one file from one location to another location in Windows XP like the code below but failed. May I know why?
copy C:/Directory_A/the_file D:/Directory_B
When I execute the batch file I see this in the output:
The syntax of the command is incorrect.
Try using backslashes
copy C:\Directory_A\the_file D:\Directory_B