Open theodumont opened 4 years ago
Maybe look for this StackOverflow post, even though I guess you have already seen it. I talks about about using psutils to check if a file is opened somewhere else :mag:
I am not sure about the problem you are trying to solve here, i.e. why are you looking if the file is opened somewhere else? What if it is?
Also, running files blindly may not be the best way to check that :exclamation: First of all because most files are not executable (so the subprocess will crash, but why not), and second of all because you have little way to control what the program will do once it has started :warning:
What if I am trying to rename WannaCry.exe :bomb: to NoTouchVirus.exe for instance? Should the default behavior be to that the tool runs the WannaCry.exe file :boom: just to check if someone else is writing to it? That seems like a risky move to me :laughing:
For reference purposes, the equivalent of
start /WAIT $file
On Linux would probably be
sh $file
# or, but less portable
bash $file
# or, without the new subprocess
./$file
# which is equivalent to (I think)
exec $file
Once again, the child sub-process will crash if the file is not executable :boom:
As a side note, Popen in Python (or subprocess.run) already creates a child process, so using start
within Popen creates a subprocess within the subprocess, which may or may not be what you want. Also, the shell=True
argument of Popen tells it to run the command within a new terminal (or shell in the Linux world). Usually this makes it more convenient to use because you can give the shell a string for its command, whereas a direct call to the OS would expect a list of strings instead.
Put graphically, Popen("start /WAIT $file", shell=True) amounts to:
The python process
|-- The Popen process
|-- A new terminal (because of shell=True)
|-- A new terminal (because of start)
|-- The process running $file
If you don't want the additional processes (but maybe you do?), one way to run the file would be to use:
from subprocess import run
# You can adapt the following line with Popen.
subprocess.run([str(file_name)])
# For instance to run cli.py with Python
subprocess.run(["python3", "cli.py"])
This should schedule the process directly, without the intermediary shells :shell:
In
functions.py
,rename_files
uses the commandexecuted by a subprocess to run a file and being able to know whether it is still open or not. This command doesn't exist on Linux, and it is a bid sad because it is the only line of code that causes Linux non being supported by the tool 😢