The most basic way to create and run a Python program is to create an empty file with a.py extension, and point to that file from the command line with python filename.py. Alternatively, you can use the default Python IDLE which comes installed with Python. You can write and execute your code inside IDLE.
Active8 months ago
How do you tell Python where to save a text file?
For example, my computer is running the Python file off my desktop. I want it to save all the text file in my documents folder, not on my desktop. How do I do that in a script like this?
Peter Mortensen14.4k1919 gold badges8888 silver badges117117 bronze badges
user1031493user1031493
5 Answers
Just use an absolute path when opening the filehandle for writing.
You could optionally combine this with
Mehdi Nellenos.path.abspath()
as described in Bryan's answer to automatically get the path of a user's Documents folder. Cheers!5,00344 gold badges2424 silver badges4242 bronze badges
How To Save Python Programs On Mac
AcornAcorn33.6k1919 gold badges118118 silver badges153153 bronze badges
Use os.path.join to combine the path to the
Documents
directory with the completeName
(filename?) supplied by the user.If you want the
Documents
directory to be relative to the user's home directory, you could use something like:Others have proposed using
os.path.abspath
. Note that os.path.abspath
does not resolve '~'
to the user's home directory:unutbuunutbu
591k112112 gold badges12861286 silver badges13271327 bronze badges
A small update to this.
raw_input()
is renamed as input()
in Python 3. der_radlerder_radler
Paul ThomasPaul Thomas
Using an absolute or relative string as the filename.
gavstargavstar
Not the answer you're looking for? Browse other questions tagged python or ask your own question.
Python is a very Mac-style language. It ditches many of the syntax conventions of traditional programming languages in favor of human-readable code, tab-based hierarchy, and abstracted memory management. It’s a great first programming language especially, which leaves a lot of folks looking for explanations on how to perform tasks that are apparently so simple, no tutorial thought to explain them. Running a Python script on macOS is often one of those things.
If you’re not familiar with the language, you can recognize scripts written in Python by their distinctive “.py” file extension. Python scripts can be executed in a couple different ways, depending on what tools you have at your disposal.
With Terminal
This most universal method uses only built-in tools and utilities.
1. Take note of the path of the Python script in Finder. File copy program for mac.
If you’re not sure what the path is, you can find out easily. Right-click on the file while holding down the Option key and select “Copy … as Pathname” to copy the path to your clipboard.
1. Use the
cd
command to navigate to the folder containing the Python file. If you copied the pathname above, paste the pathname after the cd
command.2. Type the following command to execute the script:
Of course, replace
filename
with the actual name of your script. In our example, we replaced it the filename with configure.py
.Python Launcher
By default, macOS comes with Python 2.7.3 installed. That’s not the newest version of Python, however. If you’ve installed Python 3, you’ll have access to a few more software tools. These include the Python Launcher, a GUI program that runs Python scripts. If it is installed on your Mac, you can run Python scripts from Finder.
1. Locate the Python script file in Finder.
A business card, irrespective of the nature of your business, helps you to communicate with people on daily basis. Card making programs for mac. Another brilliant feature of the Business Card Maker is its 100% Avery compatibility.
2. Right-click the file and select “Open With -> Python Launcher” from the context menu.
Alternatively, Python scripts can be dragged onto the Python Launcher icon. This works with the Python Launcher icon in both the Dock and in the Applications folder.
Configuring the Python Launcher
You can also adjust the Python Launcher with a few additional options. With these options, you can select your version of Python, enable debugging tools, and adjust how the script runs. Best genealogy software for mac 2018. These options will be mostly of interest to those with some knowledge of the Python programming language.
The Python Launcher Preferences window opens in the background automatically when Python Launcher is executed. You can also open it manually by selecting “Python Launcher -> Preferences” from the Python Launcher menubar.
The preferences window can also be invoked before running a script. Drag the script on the Python Launcher icon while holding the option key, and the preferences menu will spawn before the script is run, allowing you to adjust settings that will only apply to this run of the script. Typically, changes to the Python Launcher preferences are global, affecting all future scripts.
Conclusion
Python files can also be run within the Python IDE, IDLE. For Python programmers working within the IDE, this makes sense. But if you’re just looking to quickly launch a script, it’s like lighting a cigar with a cruise missile. Impressive, perhaps, but not what you might call “necessary.”