Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 코딩
- 급성심근경색증
- PythonProgramming
- 중도인출
- 보험료
- 납입
- 교보
- 교보생명
- jQuery
- 파이썬
- python
- 웹개발
- javascript
- 심장질환
- 추가납입
- Vue.js
- 가입
- 변환
- 보험
- 사망
- 리스트
- Java
- 인출수수료
- 문자열
- 수수료
- 프로그래밍
- 특약
- 자바스크립트
- 프론트엔드
- 뇌출혈
Archives
- Today
- Total
SeouliteLab
[Python/파이썬] 파일 개수 확인하기 본문
파일 개수를 확인하는 것은 파일 시스템 내의 파일 수를 파악하고 관리하는 데 도움이 됩니다. Python에서는 os 모듈을 사용하여 파일 개수를 확인할 수 있습니다.
1. 현재 디렉터리의 파일 개수 확인
os 모듈의 `listdir()` 함수를 사용하여 현재 디렉터리의 파일 목록을 가져옵니다. 이 목록의 길이를 통해 파일 개수를 확인할 수 있습니다.
import os
directory = '.'
file_count = len([name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))])
print("현재 디렉터리의 파일 개수:", file_count)
2. 특정 디렉터리의 파일 개수 확인
특정 디렉터리의 파일 개수를 확인하려면 해당 디렉터리 경로를 지정하여 `listdir()` 함수를 호출합니다.
import os
directory = '/path/to/directory'
file_count = len([name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))])
print("특정 디렉터리의 파일 개수:", file_count)
3. 예제 코드
import os
# 현재 디렉터리의 파일 개수 확인
directory = '.'
file_count = len([name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))])
print("현재 디렉터리의 파일 개수:", file_count)
# 특정 디렉터리의 파일 개수 확인
directory = '/path/to/directory'
file_count = len([name for name in os.listdir(directory) if os.path.isfile(os.path.join(directory, name))])
print("특정 디렉터리의 파일 개수:", file_count)
'프로그래밍' 카테고리의 다른 글
[Python/파이썬] print() 줄바꿈 없이 출력하기 (0) | 2024.03.02 |
---|---|
[Python/파이썬] 다른 파일의 변수 참조, 함수 호출하기 (0) | 2024.03.02 |
[Python/파이썬] 파일 수정 시간, 생성 시간 확인 (0) | 2024.03.02 |
[Python/파이썬] 파일 끝 찾기 (0) | 2024.03.02 |
[Python/파이썬] 텍스트 파일 이어서 쓰기 (0) | 2024.03.02 |