Try os.path.splitext it should do what you want.
import os
print os.path.splitext('/home/user/somefile.txt')[0]+'.jpg' # /home/user/somefile.jpg
os.path.splitext('/home/user/somefile.txt') # returns ('/home/user/somefile', '.txt')
More Related Contents:
- How to name a file using a value in a list in a dictionary?
- Extracting extension from filename in Python
- Why do I get a SyntaxError for a Unicode escape in my file path? [duplicate]
- How do I trim whitespace?
- Turn a string into a valid filename?
- Split by comma and strip whitespace in Python
- Remove trailing newline from the elements of a string list
- How do you import a file in python with spaces in the name?
- Strip spaces/tabs/newlines – python
- How to remove whitespaces and newlines from every value in a JSON file?
- How to strip a specific word from a string?
- How to strip all whitespace from string
- Build the full path filename in Python
- Python strip with \n [duplicate]
- How to split by comma and strip white spaces in Python?
- How can I create a full path to a file from parts (e.g. path to the folder, name and extension)?
- How can I strip first and last double quotes?
- Python threading multiple bash subprocesses?
- Keyboard Interrupts with python’s multiprocessing Pool
- _csv.Error: field larger than field limit (131072)
- Python – Using the Multiply Operator to Create Copies of Objects in Lists [duplicate]
- merging 2 dataframes vertically [duplicate]
- Fastest pairwise distance metric in python
- How to bootstrap installation of Python modules on Amazon EMR?
- Python subprocess.Popen as different user on Windows
- Normalize DataFrame by group
- Why does “%-d”, or “%-e” remove the leading space or zero?
- Why is the Python CSV reader ignoring double-quoted fields?
- How do I change the range of the x-axis with datetime?
- Filter out HTML tags and resolve entities in python