- Learn Programming in Python with Cody Jackson
- Cody Jackson
- 312字
- 2021-06-10 19:06:07
Launching Python programs
If you want to run a Python program, simply type python foo.py at the shell command prompt (make sure it's not Python's interactive prompt).
The following screenshot demonstrates how to call a Python program from the command line. This particular program simulates rolling a number of dice; the actual program will be discussed later in this book:
Files saved with the .py extension are called modules and can be called individually at the command line or imported into a program, similar to header files in other languages; we saw an example of this in the screenshot labeled Python calculator. If your program is going to import other modules, it is easiest to ensure they are all saved in the same directory on the computer, or you have to do some extra work to point to a different directory. More information on working with modules can be found in Chapter 2, Data Types and Modules, in the Importing modules section, or in the Python documentation.
Depending on the program, certain arguments can be added to the command line when launching the program. This is similar to adding switches to a Windows command prompt. The arguments tell the program what exactly it should do.
For example, perhaps you have a Python program that can output its processed data to a file rather than to the screen. To invoke this function in the program you simply launch the program like the following example—launching a Python program with arguments:
$ python foo.py -f /home/User/Documents
The -f argument is received by the program and calls a function that saves the data to the designated location (/home/User/Documents) within the computer's filesystem instead of printing it to the screen.
- JavaScript百煉成仙
- Spring技術內幕:深入解析Spring架構與設計
- INSTANT FreeMarker Starter
- C# 從入門到項目實踐(超值版)
- Julia Cookbook
- Python機器學習經典實例
- PhpStorm Cookbook
- C語言程序設計教程
- 編寫高質量代碼:改善Objective-C程序的61個建議
- Ext JS 4 Plugin and Extension Development
- 計算語言學導論
- Python 3快速入門與實戰
- C# 7.0本質論
- Mastering Machine Learning with scikit-learn
- JavaScript程序設計基礎教程(慕課版)