What renaming a .txt file to .py actually does
Renaming a text file from .txt to .py tells your computer to treat the contents as Python code instead of plain text. The file itself does not change — only the label does. If the file contains valid Python code, you can now run it as a program. If it contains random text, renaming it will not make it work; Python will throw an error when you try to run it.
The file extension is just metadata. Windows, Mac, and Linux all use it to decide which program opens the file by default and how to process it. A .txt file opens in a text editor. A .py file is recognized as executable code and can be run through a Python interpreter.
Key Takeaways
- Renaming a .txt file to .py changes only the file extension, not the contents — the code inside must already be valid Python or the file will not run.
- On Windows, right-click the file, select Rename, and change the extension from .txt to .py; on Mac and Linux, use the same method or the command line.
- After renaming, you can run the file from the command line by typing python filename.py or by double-clicking it if Python is set as the default program.
- If you see a warning that changing the extension may make the file unusable, click Yes or Proceed — this is normal and does not harm the file.
Renaming on Windows
Right-click the .txt file and select Rename from the menu. The filename will become editable. Delete the .txt at the end and type .py instead. Press Enter to confirm.
Windows may show a warning: "If you change a filename extension, the file may become unusable." This is a standard caution and does not mean anything is wrong. Click Yes to proceed. The file is not harmed — you are simply changing how Windows labels it.
If you do not see the file extension at all, Windows is hiding it. Open File Explorer, click the View tab at the top, and check the box next to File name extensions. Now you will see .txt and can rename it to .py.
Renaming on Mac and Linux
Right-click the file and select Rename (Mac) or Rename from the context menu (Linux desktop). Change the extension from .txt to .py and press Enter.
From the terminal or command line, use the mv command to rename the file. Type mv filename.txt filename.py and press Enter. This method is faster if you are already working in the terminal. On Mac, you can also open Finder, select the file, press Enter, and edit the name directly.
Running the file after renaming
Open a terminal or command prompt and navigate to the folder where the file is stored. Type python filename.py (or python3 filename.py on some systems) and press Enter. Python will read the file and run the code inside.
If you get an error like "python is not recognized" or "command not found," Python is not installed on your system or not added to your system path. You will need to install Python first from python.org or through your system package manager before you can run .py files.
On Windows, you can also double-click the .py file to run it directly if Python is set as the default program for .py files. On Mac and Linux, double-clicking usually opens the file in a text editor instead; use the terminal method above.
What happens if the code inside is not valid Python
If the .txt file contains text that is not Python code — random words, notes, data in another format — renaming it to .py will not make it work. When you try to run it, Python will show an error message pointing to the line it does not understand.
The error message is useful: it tells you exactly what Python could not parse and where. Read it carefully. Common mistakes include misspelled keywords, missing colons at the end of lines, or incorrect indentation. Fix the code in a text editor, save the file, and try running it again.
Checking your file before renaming
Before you rename a .txt file to .py, open it in a text editor and look at the contents. If it looks like Python code — with words like def, if, for, import, or print — it is probably safe to rename. If it is just notes or data, renaming will not help.
You can also test the file by opening a terminal, navigating to its folder, and typing python -m py_compile filename.txt. This checks whether the contents are valid Python without actually running the code. If there are errors, Python will tell you what is wrong before you rename the file.
Frequently Asked Questions
Will renaming a .txt file to .py delete or corrupt it?
No. Renaming changes only the label, not the contents. The data inside stays exactly the same. You can rename it back to .txt at any time and it will work as before.
What if I cannot see the file extension when I right-click?
On Windows, file extensions are often hidden by default. Go to View in File Explorer and turn on "File name extensions." On Mac and Linux, extensions are usually visible, but if not, use the terminal command mv filename.txt filename.py instead.
Do I need to do anything else after renaming besides running the file?
No. Once renamed to .py, the file is ready to run. Make sure the code inside is valid Python, then run it from the terminal using python filename.py.
Can I rename a file to .py if it is currently open in an editor?
It depends on the editor. Most text editors will let you rename the file while it is open, but some may lock it. Close the file first to avoid any issues, rename it, then open it again.
What is the difference between python and python3 when running the file?
On most modern systems, python3 runs Python version 3, which is the current standard. Older systems may use python for version 2, which is no longer supported. If python filename.py does not work, try python3 filename.py instead.