I want to open a file in LibreOffice Calc on Ubuntu after working on it through Python.
How would I go about this?
I tried:
import subprocess
subprocess.call("explorer path-to-file")
# got error that the path doesn't exist
subprocess.call("calc path-to-file")
# Calc is not executable type error
You could use the xdg-open tool (if you have it) to use the default tool for the file type, or if you really want to always use Libreoffice, the executable for it is libreoffice.
os.system("xdg-open path-to-file")
os.system("libreoffice path-to-file")
(and as always when using os.system(), make sure path-to-file comes from a trusted source.)
Related
In a GUI app written in Python and Tkinter, I want to add a menu command which will open the source code of the *.py file in IDLE. I also want it to be platform independent.
I've tried using os.system to open IDLE from Scripts folder of Python, but that would be platform dependent. I couldn't find a way to get the Scripts folder of Python to make it independent. How can I achieve this?
To open a file in an IDLE editor, the command line is
<python> -m idlelib <path>
where <python> is the full path to a python executable or a name that resolves to such. If you want to open IDLE with the same python that is running you python app, which is likely what you want, use the platform-independent sys.executable. The path to the source code for the running app is __file__. This is one of the semi-hidden global variables when python runs a file. Or give the path to any other file.
Whether you use os.system or subprocess is a different issue. The latter is more flexible. subprocess.run replaces os.system. subprocess.Popen should not block. IDLE uses the latter to run user code.
I tried some special way:
import idlelib.pyshell
import sys
sys.argv.clear()
sys.argv.append("")
sys.argv.append(fName)
idlelib.pyshell.main()
It works! But I don't know whether this way will generate some problems.
Alright, so here's the problem: I'm trying to make a .pyw file in which the exception handler opens a .py terminal. As far as I understand, you need to use a subroutine. However, I really can't get my head around that. Here's that I tried to do.
except:
subprocess.run('alphareset.py')
Also, I have 2 local variables in a file that have the same name. How would I import them into other files?
You need to specify the executable:
subprocess.run('python alphareset.py')
The association of the extension *.py with the Python executable on Windows typically does not work from within a subprocess.
I have recently started using notepad++ for my coding in various different programming languages. In Python specifically, I have tried to open a basic text file (.txt), and print the text inside the file. If I run this file via just double clicking it (in Windows Explorer, not in Notepad++), it will run, and do exactly what I want it to do; print the contents of the text file. However, if I try and run it directly from Notepad++ with the line
C:\Python25\python.exe "$(FULL_CURRENT_PATH)"
then it comes up with an error telling me 'it cannot find the file specified'.
I am under the suspicion that it is using its own directory to try and find the files, so if that is the case is it possible to change it to the files directory?
C:\Python25\python.exe probably isnt your path to python ... I think you just copied it from somewhere
you should probably put the path to your python installation instead (I would guess its something like C:\Python27\python.exe
I am making an app in python, which is able to open different file types. This code is running fine on eclipse while passing filename which I want to open and configuration file as arguments respectively selectedFileName=(sys.argv)[1]
cfgFile=(sys.argv)[2]. Now I converted this into application by using py2app. So, the issue is how to deal with arguments, as different file types need to be open through app and this app also needs configuration file while processing. Through py2app, in terminal passing this command open -a myapp.app selectedFileName config.cfg opens the file as expected. But, What I want is to directly open file of any extension without the use of terminal. Is openwith for opening file possible in this case, then how?. What changes I have to make in code for passing arguments for both? I also want to distribute this app to others.
You can use py2app. It makes a standard app. All it needs is a python script and setup.py.
Platypus
You might want to have a look at Platypus. It's intended to create Mac .app bundles from shell scripts. The documentation has some information on how to accept files that looks like it should be applicable to you. I haven't tested it out, but you should be able to run your script by dropping files onto the icon, and possibly using the Open With menu as well.
I have written a program in python that is another front end for un-tarring files on OSX. I want to be able to double click on the file and open with the program that I wrote. How do I make the program automatically know that it is being opened with that file, and to work on that file?
You will need to make your make your script executable with the command chmod a+x <your script.py>
You will also need to tell your OS that the python interpreter is needed to execute this file. In Linux the line #!/usr/bin/env python at the top of the file is what does this. I assume its the same in OSX.
Then right click and select "Open with" and then "Other...". And select your script.
The Python script will need to be configured correctly to run when the OS calls it in this way.
Another way to do it(Caveat: I haven't personally tried this) is to use a shell script as advised in this anwer on the superuser forums.
Python scripts on OS X are not by default recognized as launchable applications by the Finder. What you need to do is use a tool that will package your Python script as a Mac OS X application. Then, you can right-click or ctrl-click a file of your chosen file type and locate that application through the Open With... dialog.
One option I'm aware of for doing this is part of the MacPython package, called py2app:
http://wiki.python.org/moin/MacPython/py2app
So, your steps would be:
1) Use py2app to package your Python script as a launchable application
2) Associate the application with the file type you'd like it to open using by right-clicking or ctrl-clicking one of that file type and choosing "Open With..." in the OS X Finder.
import sys
print("Program.py executing...")
for x in sys.argv:
print(x)
Produces the output
Program.py executing...
Program.py
something.tar
I'm windows, running this command:
python Program.py something.tar
I don't think anything changes from that command to OSX.
And as said by benjamin, the OS is responsible for determining what program is used for the file. I don't know what the command line from your os will be, but you can find out with the above program.