I've made a little Python (3.x) script and compiled it to a *.exe file using Py2Exe.
What I would like is to click on a random file in explorer and "open it with..." (using the right mouse button) my executable. My program can then use the path of the selected file.
I know such information is typically passed into 'argv[...]', however, it is not working. I only get one argument, the full path of the .exe file.
For now the program only creates a *.txt file with all the passed arguments.
Could anyone help me out?
Thanks in advance.
The full code:
import sys
filename = "Test.txt"
file = open(filename, 'w')
file.write('Number of arguments: ' + str(len(sys.argv)) + ' arguments.\n')
file.write('Argument List: ' + str(sys.argv))
file.close()
I now tried adding an input line at the end of my script to prevent it from closing instantly and I noticed an error in the panel: "ImportError: No module named 'ctypes'". Some searching showed this is a bug in py2exe: How to stop Python program compiled in py2exe from displaying ImportError: No Module names 'ctypes'
The mentioned solution solved the error and after rebuilding the *.exe passing in the file path works! So it was a py2exe bug all along...
Related
I am trying to use python subprocess to call an exe. The application usually takes the parameter file from the same directory as exe. However, as the python file is not located at the same directory as exe, the exe cannot find the parameter file when called by subprocess.run. Hence I specified the cwd when calling subprocess.run like below:
subprocess.run([cwd_exe, "--cal-cn-bv", cwd_cif, "Cs1"], cwd=r'd:\Study\softBV_mix\GitHub\projects\Coord\bin', capture_output=True)
However the subprocess still cannot find the dat file in
d:\Study\softBV_mix\GitHub\projects\Coord\bin
The error message appears as
CompletedProcess(args=['d:\Study\softBV_mix\GitHub\projects\Coord\bin/softBV0405.exe',
'--cal-cn-bv',
'd:\Study\softBV_mix\GitHub\projects\Coord\test/CsCl.cif',
'Cs1'], returncode=0, stdout=b'Warning: unable to find
d:\Study\softBV_mix\GitHub\projects\Coord\database_unitary.dat
where the database_unitary.dat is supposed to be in .../coord/bin/. The application works well if I call it from powershell or command prompt.
No one has answered my question but I kind of found the workaround myself though I am sure if I identify the root cause correctly.
In the end I imported os and make sure cwd is the recognised absolute addresss
import os
cwd = os.path.abspath("../bin")
This worked out.
So the expression
r'd:\Study\softBV_mix\GitHub\projects\Coord\bin'
make cause the issue. Hope some PRO can further clarify this.
My problem is that when I pack my .py script with either pyarmor or pyinstaller the script can't find my files anymore. I am using this code to read a file:
with open('./file.txt', "r") as textfile:
textdata = textfile.read()
print(textdata)
I found out that the script uses /Users/{username} as absolute file path which is my problem. Because no matter where the file is started it always uses this /Users/{username} path. I even tried getting the file path with:
workingpath = str(Path().absolute())
But even that returns /Users/{username}. Is there any solution to this problem?
Thanks in advance for any help.
I am trying to set my Python script as default program to open a file (e.g. open every .txt file with my program when I double click on it).
I already tried this:
from sys import argv
# write the arguments to a file for debugging purposes
with open("output.txt", "w+") as f:
f.write(repr(argv))
I converted the script into a .exe with pyinstaller, otherwise Windows won't let me use it to open files.
In the command prompt, it works: typing main.exe some args indeed yields an output.txt file, with inside it ["C:\...\main.exe", "some", "args"].
I was hoping that by opening a .txt file with this script (in File Explorer > right click on file > open with > more apps > check "always use this app" and selecting the executable), it would be the same as running main.exe C:\...\that_file_that_i_just_clicked.txt in the command prompt, from which I could then use the file path to open it in my program. However, this does not happen. In fact, main.exe never even gets executed (because it doesn't even create a new output.txt).
How can I link a pyinstaller-generated executable to always open a filetype, and how do I then know the path of the opened file in Python?
The thing that I was doing wrong, was creating output.txt using a relative file path. Since the script was converted into an .exe (which basically wraps the interpreter and the script into a single file), the relative file path stopped working.
Using an absolute file path fixed my issue (as pointed out by Eryk Sun).
I am using sqlite3 and python for a new website I am creating. The problem is, the "files_storage.db" file I am trying to create will not appear in any Windows 10 Folder Window, PyCharm's Directory View, nor will it appear via the command line interface.
The catch to this is, if I execute my python script multiple times, I get an error that states the database file already exists... So this file is somewhere, I guess it is a game of cat and mouse to find it.
I have ran into this problem before, but I have ALWAYS been able to find the file via the command line. Usually, I wouldn't both yall with such a question but, this is really irking me and I am going to run into serious issues when it comes time to put this baby on a server. :(
thanks in advance, and here's some screenshots I suppose.
You are using a relative file path.
Relative paths are converted to absolute ones by something like os.path.join(os.getcwd(), <relative path>). So they depend on the current working directory of the process.
Try to open it with an absolute path (starting with drive letter) to avoid any ambiguities.
If you use just a filename without a path, the file will be saved in whatever the current working directory of the Python interpreter is.
To see where the current working directory is, add the following code to the beginning of your program:
import os
print(os.getcwd())
You should then see the working directory in the output.
There is a setting for the current working directory in your IDE somewhere. See e.g. the answers to this question.
You can also do something like:
import os
path = os.path.expanduser("~") + '/Documents'
print(path)
This will allow you to access the directories for the current user. For me, this prints:
'/Users/thomasweeks/Documents'
I am writing a Python program which reads data from a .txt file and writes to it. I am using VS Code as my editor and I am coming across an error where it doesn't recognize infile = open("poem.txt", "r") and tells me there is no such file or directory poem.txt. I can assure you it does exist and it is in the same folder. I can open the same program within IDLE and it runs fine. Is there a way to correct this error for VS Code without trying to hard code in absolute path for the file?
This code is not hardcoded, but will change with the directory the program is run from:
Join constructs file paths from arguments, and getcwd gets the current working directory.
import os
infile = open(os.join(os.getcwd(), "poem.txt"), "r")