Getting the name of the file without the extension:
import os print(os.path.splitext("/path/to/some/file.txt")[0])
Prints:
/path/to/some/file
Documentation for os.path.splitext
.
Important Note: If the filename has multiple dots, only the extension after the last one is removed. For example:
import os print(os.path.splitext("/path/to/some/file.txt.zip.asc")[0])
Prints:
/path/to/some/file.txt.zip
See other answers below if you need to handle that case.
Related Posts:
- working of \n in python [duplicate]
- How to remove punctuation in python?
- Python: TypeError: cannot concatenate ‘str’ and ‘int’ objects
- sprintf like functionality in Python
- n-grams in python, four, five, six grams?
- Alphabet range in Python
- How do I lowercase a string in Python?
- How do I get a substring of a string in Python?
- Does Python have a string ‘contains’ substring method?
- Convert bytes to a string
- Replacing instances of a character in a string
- Changing one character in a string
- How to read a file line-by-line into a list?
- What is lexicographical order?
- strip(char) on a string
- How to convert list to string [duplicate]
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- Converting integer to string in Python
- Reverse a string in Python
- How do I compare two strings in python?
- How can I safely create a nested directory in Python?
- Regex not operator
- ValueError: could not convert string to float: id
- Regex not operator
- Print string to text file
- Best way to convert string to bytes in Python 3?
- Pythonic way to create a long multi-line string
- Best way to convert string to bytes in Python 3?
- Does Python have a string ‘contains’ substring method?
- Extract a substring using PowerShell
- TypeError: not all arguments converted during string formatting python
- Graphviz’s executables are not found (Python 3.4)
- Extract substring in Bash
- Regex not operator
- ‘str’ object does not support item assignment
- How do I append one string to another in Python?
- How do I append one string to another in Python?
- Random string generation with upper case letters and digits
- ow can I find where Python is installed on Windows?
- How can I split and parse a string in Python?
- Graphviz’s executables are not found (Python 3.4)
- Importing modules from parent folder
- How to read a text file into a string variable and strip newlines?
- What is the difference between \r\n, \r, and \n? [duplicate]
- Convert bytes to a string
- How to print like printf in Python3?
- Importing modules from parent folder
- string to string array conversion in java
- How can I safely create a nested directory in Python?
- How to read a text file into a string variable and strip newlines?
- What is the difference between \r\n, \r, and \n? [duplicate]
- Python convert tuple to string
- Regex how to match an optional character
- write() versus writelines() and concatenated strings
- Why is it string.join(list) instead of list.join(string)?
- Why is it string.join(list) instead of list.join(string)?
- Graphviz’s executables are not found (Python 3.4)
- Split a string into 2 in Python
- ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL’s are there
- Best way to strip punctuation from a string
- ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL’s are there
- Split string with multiple delimiters in Python [duplicate]
- How to check if the string is empty?
- How to find all occurrences of a substring?
- How do I properly compare strings in C?
- How to remove .html from URL?
- Split string on whitespace in Python
- What is the difference between a “line feed” and a “carriage return”?
- How to convert an int to string in C?
- How do I convert a list of ascii values to a string in python?
- How to check whether a str(variable) is empty or not?
- Python TypeError: not enough arguments for format string
- What does the ‘b’ character do in front of a string literal?
- Ignoring upper case and lower case in Java
- Python Setup Disabling Path Length Limit Pros and Cons?
- How to add a string in a certain position?
- Check string “None” or “not” in Python 2.7
- Remove all special characters, punctuation and spaces from string
- This can be done without regex:
- How can I concatenate str and int objects?
- What does the ‘b’ character do in front of a string literal?
- How to replace all double quotes to single quotes using jquery?
- Pythonic way to create a long multi-line string
- Python can’t find my module
- Best way to replace multiple characters in a string?
- Convert character to ASCII numeric value in java
- Convert columns to string in Pandas
- Relative paths in Python
- What does strcmp() exactly return in C?
- How to get an absolute file path in Python
- Generate random string/characters in JavaScript
- “[ ]” vs. “[[ ]]” in Bash shell
- How to delete a character from a string using Python
- Python: Get the first character of the first string in a list?
- How to trim whitespace from a Bash variable?
- How to convert string to binary?
- Remove the first character of a string
- Convert hex to binary
- Quoting backslashes in Python string literals
- Python list directory, subdirectory, and files