To remove an extension from a file path?

Asked 2 years ago, Updated 2 years ago, 82 views

os.path.You can remove the extension by using the basename I imported the os module path.Unable to access basename.

os.path.Is there any other way besides basename ?

string python path

2022-09-22 10:49

1 Answers

You can import os but os.path.It's a little strange that I can't write bassname.

Maybe it's because you took a weird approach, so try it this way

from os.path import basename
print basename("/a/b/c.txt")

os.path.To remove the extension without using basename, you must use os.path.splitext(path).

os.path.splitext(path) divides the path of the file by (root, ext) (but root+ext = path) ext is either an empty string or a string containing one maximum point (only the last ext2 split if path is myfile.ext1.ext2) And if path starts with a point (.myfile), the beginning is included in root.

import os
print os.path.splitext("path_to_file")[0]

There is.


2022-09-22 10:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.