프로그래밍 언어/Python
Python의 경로에서 확장자없이 파일 이름을 얻는 방법
Rateye
2021. 6. 29. 10:02
728x90
반응형
질문 : Python의 경로에서 확장자없이 파일 이름을 얻는 방법은 무엇입니까?
Python의 경로에서 확장자없이 파일 이름을 얻는 방법은 무엇입니까?
예를 들어, "/path/to/some/file.txt"
"file"
합니다.
답변
확장자없이 파일 이름 얻기 :
import os
print(os.path.splitext("/path/to/some/file.txt")[0])
결과물 :
/path/to/some/file
중요 참고 : 파일 이름에 여러 개의 점이있는 경우 마지막 점 이후의 확장자 만 제거됩니다. 예를 들면 :
import os
print(os.path.splitext("/path/to/some/file.txt.zip.asc")[0])
결과물 :
/path/to/some/file.txt.zip
이 경우를 처리해야하는 경우 아래의 다른 답변을 참조하십시오.
출처 : https://stackoverflow.com/questions/678236/how-to-get-the-filename-without-the-extension-from-a-path-in-python
728x90
반응형