반응형
파일이 python에서 디렉토리인지 일반 파일인지 확인하는 방법
경로가 python의 디렉토리인지 파일인지 어떻게 확인합니까?
os.path.isfile("bob.txt") # Does bob.txt exist? Is it a file, or a directory?
os.path.isdir("bob")
사용하다os.path.isdir(path)
자세한 내용은 이쪽 http://docs.python.org/library/os.path.html을 참조해 주세요.
Python 디렉토리 함수의 대부분은 모듈에 포함되어 있습니다.
import os
os.path.isdir(d)
통계 문서의 교육용 예:
import os, sys
from stat import *
def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print 'Skipping %s' % pathname
def visitfile(file):
print 'visiting', file
if __name__ == '__main__':
walktree(sys.argv[1], visitfile)
언급URL : https://stackoverflow.com/questions/3204782/how-to-check-if-a-file-is-a-directory-or-regular-file-in-python
반응형
'itsource' 카테고리의 다른 글
Python에서 **kwargs를 사용하는 올바른 방법 (0) | 2022.12.24 |
---|---|
JavaScript로 끝남 (0) | 2022.12.24 |
@Transactional annotation: 인터페이스 정의 또는 구현 클래스 어디에 배치해야 합니까? (0) | 2022.12.24 |
페이지 새로 고침 없이 폼 데이터를 PHP로 전달하는 AJAX와 함께 폼 제출 (0) | 2022.12.24 |
ASSERTION을 생성하려고 할 때 설명할 수 없는 mysql 오류가 발생했습니다. (0) | 2022.12.24 |