To remove an extension from a file path?

Asked 1 years ago, Updated 1 years ago, 72 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.