- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to change file extension in Python?
When changing the extension, you're basically just renaming the file and changing the extension. In order to do that, you need to split the filename by '.' and replace the last entry by the new extension you want. You can do this using the os.rename method.
>>> import os >>> my_file = 'my_file.txt' >>> base = os.path.splitext(my_file) >>> os.rename(my_file, base + '.bin')
This will rename my_file.txt to my_file.bin
- Related Articles
- How to extract file extension using Python?
- Change the file extension in the text column in MySQL?
- How to write Python regular expression to match with file extension?
- How to change file permissions in Python?
- Perl File Extension
- How to get the file extension using PowerShell?
- Get file extension name in Java
- How to get file extension of file as a result of MySQL query?
- Golang program to get the file extension
- How to change the permission of a file using Python?
- How to change the mode of a file using Python?
- How to change the owner of a file using Python?
- C# program to get the extension of a file in C#
- Create temporary file with specified extension suffix in Java