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
- Changing one character in a string
- How to read a file line-by-line into a list?
- strip(char) on a string
- How can I safely create a nested directory in Python?
- Regex not operator
- Best way to convert string to bytes in Python 3?
- Extract a substring using PowerShell
- Graphviz’s executables are not found (Python 3.4)
- Extract substring in Bash
- ‘str’ object does not support item assignment
- ow can I find where Python is installed on Windows?
- Graphviz’s executables are not found (Python 3.4)
- 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]
- Why is it string.join(list) instead of list.join(string)?
- ImportError: DLL load failed: %1 is not a valid Win32 application. But the DLL’s are there
- 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 find all occurrences of a substring?
- How to remove .html from URL?
- 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?
- Python TypeError: not enough arguments for format string
- What does the ‘b’ character do in front of a string literal?
- Check string “None” or “not” in Python 2.7
- 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?
- Best way to replace multiple characters in a string?
- Convert character to ASCII numeric value in java
- How to get an absolute file path in Python
- Generate random string/characters in JavaScript
- 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
- Open file in a relative location in Python
- Why am I getting a FileNotFoundError?
- Remove all special characters, punctuation and spaces from string
- How do I remove a substring from the end of a string?
- How to extract the substring between two markers?
- ValueError: cannot index with vector containing NA / NaN values
- How to trim whitespace from a Bash variable?
- Insert some string into given string at given index in Python
- How to check if type of a variable is string?
- How do I format a string using a dictionary in python-3.x?
- Removing numbers from string
- Count the number of occurrences of a character in a string
- What is the difference between ‘ and ” in JavaScript?
- Center text in fixed-width field with stream manipulators in C++
- How to find char in string and get all the indexes?
- C# – Illegal characters in path
- \n or \n in php echo not print [duplicate]
- PadLeft function in T-SQL
- How can I check if character in a string is a letter? (Python)
- Replacing a character from a certain index
- Find the nth occurrence of substring in a string
- How to convert ‘binary string’ to normal string in Python3?
- Windows path in Python
- Python: Converting string into decimal number
- Lua read beginning of a string
- TypeError: decoding str is not supported
- How does str(list) work?
- PHP to write Tab Characters inside a file?
- How to change pip installation path
- What is the difference between a string and a byte string?
- Split string using a newline delimiter with Python
- Haskell: Converting Int to String
- Remove specific characters from a string in Python
- How to remove first character from C-string?
- Difference between char* and char** (in C)
- Using strip() to clean up a string
- Extract filename and extension in Bash
- Meaning of #{ } in Ruby?
- Python code to remove HTML tags from a string
- How can I check if a string contains ANY letters from the alphabet?
- Taking a new line using printf in java? Is %n correct?
- Find index of last occurrence of a substring in a string
- Python convert tuple to string
- How to count instances of character in SQL Column
- TypeError: coercing to Unicode: need string or buffer