Python open a text(notepad) document - python

Im new at programming and i was wondering how can i open a notepad document so the user can type on it. I already know that you can use
variablename = open("filename.txt","w")
to open and write on a file but instead of writing on python i wanted the file to open directly so the user can type on the actual file not in python shell.
So far i know that i have to use
import os
os.?????(filename.txt)
but i dont know how to make the file pop up so the user can enter data. can somebody help me?

This easiest approach using os is to use os.system to run a shell script:
import os
os.system("notepad filename.txt")
Or using subprocess.Popen which is usually the recommended way:
import subprocess
subprocess.Popen(["notepad","filename.txt"])
# the concepts of both my methods is they run a shell script calling notepad to run filename.txt
But I believe only Windows has the Notepad application.
You can also use the suggested method from the comments:
import subprocess
subprocess.run(["notepad","filename.txt"])
But that only works in Python 3.5+

from os import startfile
startfile( "filename.txt" )
this will open your file in your system's default editor for the file type.

import os
os.system("start notepad.exe <path/to/file>")
This will pop-up Notepad. If the file doesn't exist, there will be a prompt asking if you'd like to create it.

Related

Is it possible to write to Command Prompt with python?

Im opening Command Prompt with
os.startfile('C:\\WINDOWS\\system32\\cmd.exe')
and after opening the program id like to write the python file for it to run
C:\Users\user\Documents\Python>examplefile.py
when python opens the command prompt it starts with
C:\Users\user\Documents\Python>
so I would just need to add on the file to the end of the line and run it, is this possible?
You can use the os module to open cmd and the keyboard module for writing in cmd
import os
import keyboard
import time
os.system("start cmd")
# Because we don't want the `keyboard` module to write before cmd gets opened.
time.sleep(0.1)
keyboard.write("Anything you want")
I would suggest you to use os.startfile (this link is for 2.7 documentation)
Otherwise importing keyboard and using keyboard.write("python3 myfile.py") should work.

How can I open the currently running script's .py file using IDLE in Python?

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.

How to use a python IDE as a substitute for command prompt?

It is possible to open a program on your pc using the command prompt. Is there a way to use a Python IDE or an idea for code that helps me do that?
You can use:
import os
os.startfile("application_name.exe")
Or if you want to run external applications too, you need to check the path for this application.
import os
os.chdir(r'C:\program files\programfolder')
os.startfile("application_name.exe")

shell command in python to run a specific windows executable

I have been searching but I am so confused. I so appologize if this has been answered before but I have looked and I am even more confused. All I want to do is run an executable file from a python script.
I know to open notepad.exe (for instance) you do the following.
#opens notepad in windows
import os
print os.system('notepad.exe')
but what if I want to run something specific in a directory
How can I effectively run something like this (this is obviously going to fail)
#opens program in windows
import os
print os.system('c:\files\program.exe')
the more I read about it, the more confused I get.
I have been reading about sys.executable as well as surprocess but its confusing me more than helping. Could someone clarify how this can be done? An example, perhaps to run a "program.exe" file?
You can use os.system like that. Note that strings require proper escaping though, so you might need to escape those backslash characters. Alternatively, you can also use a raw string to make it work:
os.system(r'c:\files\program.exe')
You can also use subprocess module https://docs.python.org/2/library/subprocess.html
import subprocess
subprocess.call('c:\files\program.exe')

Opening a file, and taking input from the file opened in a Python program

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.

Categories

Resources