Linux下无需IDE,使用纯文本编辑器操作Python文件的方法
Hey there! Let's break down exactly how to handle each of your tasks using basic text editors (like nano, vim, or even the graphical gedit) and Linux terminal commands—no IDE required. Here's step-by-step guidance:
1. Create a Python File
You’ve got two straightforward options here:
- Option 1: Create an empty file via terminal first
Run this command to make a blank Python file namedmy_first_script.py:touch my_first_script.py - Option 2: Create and open the file directly with a text editor
If you want to jump straight into writing, use your editor of choice. For example, withnano(a beginner-friendly terminal editor):
This creates the file if it doesn’t exist and opens it immediately.nano my_first_script.py
2. Write the Python File
Once you’ve opened the file in your editor:
- For
nano: Start typing your code right away (e.g.,print("Hello from Linux terminal!")). When finished, pressCtrl+Oto save, hit Enter to confirm the filename, thenCtrl+Xto exit. - For
vim: Pressito enter insert mode, type your code, then pressEscto exit insert mode. Type:wqand hit Enter to save and quit. - For graphical editors like
gedit: Just type your code, click the save button, and close the window when done.
3. Copy and Modify an Existing Python File
First, copy the file from its original location to your current working directory (or wherever you want to edit it) using the cp command:
cp /path/to/your/old_course_script.py ./modified_script.py
Replace /path/to/your/old_course_script.py with the actual file path (you can use pwd to get the current folder’s path, or drag-and-drop the file into the terminal to auto-fill the path).
Next, open the copied file to make your small changes:
nano modified_script.py
Edit the code as needed (e.g., tweak a variable value, change a print statement, or adjust a loop condition), then save and exit the editor like you did in step 2.
4. Run the Modified Python File
Head back to the terminal and run the script using Python 3 (the standard version these days):
python3 modified_script.py
If you get an error saying python3 isn’t found, install it with your distro’s package manager (e.g., sudo apt install python3 for Debian/Ubuntu, sudo dnf install python3 for Fedora).
内容的提问来源于stack exchange,提问作者Tree Garen

