Skip to main content
added 13 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

In Python 3.4 or newer (or as a separate backport install), you can also use the pathlib library, which offers a more object-oriented approach to path handling. pathlib.Path() objects have a .stem attribute, which is the final component without the extension suffix:

try: import pathlib except ImportError: # older Python version, import the backport instead import pathlib2 as pathlib basename = pathlib.Path(f.name).stem 

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.basename(a) 'test.txt' >>> os.path.splitext(os.path.basename(a)) ('test', '.txt') >>> os.path.splitext(os.path.basename(a))[0] 'test' >>> import pathlib >>> pathlib.Path(a) PosixPath('/home/user/Downloads/repo/test.txt') >>> pathlib.Path(a).stem 'test' 

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

In Python 3 or as a separate backport install, you can also use the pathlib library, which offers a more object-oriented approach to path handling. pathlib.Path() objects have a .stem attribute, which is the final component without the extension suffix:

try: import pathlib except ImportError: # older Python version, import the backport instead import pathlib2 as pathlib basename = pathlib.Path(f.name).stem 

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.basename(a) 'test.txt' >>> os.path.splitext(os.path.basename(a)) ('test', '.txt') >>> os.path.splitext(os.path.basename(a))[0] 'test' >>> import pathlib >>> pathlib.Path(a) PosixPath('/home/user/Downloads/repo/test.txt') >>> pathlib.Path(a).stem 'test' 

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

In Python 3.4 or newer (or as a separate backport install), you can also use the pathlib library, which offers a more object-oriented approach to path handling. pathlib.Path() objects have a .stem attribute, which is the final component without the extension suffix:

try: import pathlib except ImportError: # older Python version, import the backport instead import pathlib2 as pathlib basename = pathlib.Path(f.name).stem 

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.basename(a) 'test.txt' >>> os.path.splitext(os.path.basename(a)) ('test', '.txt') >>> os.path.splitext(os.path.basename(a))[0] 'test' >>> import pathlib >>> pathlib.Path(a) PosixPath('/home/user/Downloads/repo/test.txt') >>> pathlib.Path(a).stem 'test' 
added 437 characters in body
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

In Python 3 or as a separate backport install, you can also use the pathlib library, which offers a more object-oriented approach to path handling. pathlib.Path() objects have a .stem attribute, which is the final component without the extension suffix:

try: import pathlib except ImportError: # older Python version, import the backport instead import pathlib2 as pathlib basename = pathlib.Path(f.name).stem 

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.basename(a) 'test.txt' >>> os.path.splitext(os.path.basename(a)) ('test', '.txt') >>> os.path.splitext(os.path.basename(a))[0] 'test' >>> import pathlib >>> pathlib.Path(a) PosixPath('/home/user/Downloads/repo/test.txt') >>> pathlib.Path(a).stem 'test' 

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.splitext(os.path.basename(a))[0] 'test' 

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

In Python 3 or as a separate backport install, you can also use the pathlib library, which offers a more object-oriented approach to path handling. pathlib.Path() objects have a .stem attribute, which is the final component without the extension suffix:

try: import pathlib except ImportError: # older Python version, import the backport instead import pathlib2 as pathlib basename = pathlib.Path(f.name).stem 

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.basename(a) 'test.txt' >>> os.path.splitext(os.path.basename(a)) ('test', '.txt') >>> os.path.splitext(os.path.basename(a))[0] 'test' >>> import pathlib >>> pathlib.Path(a) PosixPath('/home/user/Downloads/repo/test.txt') >>> pathlib.Path(a).stem 'test' 
Source Link
Martijn Pieters
  • 1.1m
  • 326
  • 4.2k
  • 3.4k

Use the os.path module to work with paths; the os.path.basename() function gives you the last part after the last path separator, and os.path.splitext() gives you the filename with the extension split off:

import os.path basename = os.path.splitext(os.path.basename(f.name))[0] 

Using the os.path functions ensures that your code will continue to work correctly on different operating systems, even if the path separators are different.

Demo:

>>> import os.path >>> a = "/home/user/Downloads/repo/test.txt" >>> os.path.splitext(os.path.basename(a))[0] 'test'