코드 라인을 계산할 프로젝트가 있습니다. 파이썬을 사용하여 프로젝트가 포함 된 파일 디렉토리의 모든 코드 줄 수를 계산할 수 있습니까?Python을 사용하여 디렉토리의 코드 행을 계산하십시오.
2
A
답변
3
from os import listdir
from os.path import isfile, join
def countLinesInPath(path,directory):
count=0
for line in open(join(directory,path), encoding="utf8"):
count+=1
return count
def countLines(paths,directory):
count=0
for path in paths:
count=count+countLinesInPath(path,directory)
return count
def getPaths(directory):
return [f for f in listdir(directory) if isfile(join(directory, f))]
def countIn(directory):
return countLines(getPaths(directory),directory)
디렉토리의 파일에서 모든 코드 행을 계산하려면 디렉토리를 매개 변수로 전달하여 "countIn"함수를 호출하십시오.
+0
파이썬에 이미'len (file.readlines())'가 없습니까? 그리고 그것은 내가 아는 한 가지 방법 일뿐입니다. –
+0
그래도 작동 할 수 있다고 생각합니다. – Daniel
0
다음은 파이썬 패키지의 모든 코드 행을 계산하고 유익한 결과를 출력하기 위해 작성한 함수입니다. . 그것은
import os
def countlines(start, lines=0, header=True, begin_start=None):
if header:
print('{:>10} |{:>10} | {:<20}'.format('ADDED', 'TOTAL', 'FILE'))
print('{:->11}|{:->11}|{:->20}'.format('', '', ''))
for thing in os.listdir(start):
thing = os.path.join(start, thing)
if os.path.isfile(thing):
if thing.endswith('.py'):
with open(thing, 'r') as f:
newlines = f.readlines()
newlines = len(newlines)
lines += newlines
if begin_start is not None:
reldir_of_thing = '.' + thing.replace(begin_start, '')
else:
reldir_of_thing = '.' + thing.replace(start, '')
print('{:>10} |{:>10} | {:<20}'.format(
newlines, lines, reldir_of_thing))
for thing in os.listdir(start):
thing = os.path.join(start, thing)
if os.path.isdir(thing):
lines = countlines(thing, lines, header=False, begin_start=start)
return lines
그냥 디렉토리를 통과, 그것을 사용하려면 당신이 시작하고 싶은 모든 평에서 모든 행을 계산합니다 예를 들어, 일부 패키지 foo
코드의 라인을 계산 :
countlines(r'...\foo')
어느 것과 같은 결과물을 출력 할 것입니다
ADDED | TOTAL | FILE
-----------|-----------|--------------------
5 | 5 | .\__init__.py
539 | 578 | .\bar.py
558 | 1136 | .\baz\qux.py
관련 문제
- 1. git을 사용하여 논리적 코드 행을 계산하십시오.
- 2. Python을 사용하여 파일의 짝수를 계산하십시오.
- 3. wc를 사용하여 perl의 # 행을 계산하십시오.
- 4. MySQL 행을 계산하십시오.
- 5. MongoDB 컬렉션 행을 계산하십시오.
- 6. 열의 모든 행을 계산하십시오.
- 7. 디렉토리의 각 파일에있는 마크 다운 링크를 계산하십시오.
- 8. Python을 사용하여 파일 이동
- 9. 특정 디렉토리의 파일 및 폴더 수를 계산하십시오.
- 10. Android - 서버 디렉토리의 파일 수를 계산하십시오.
- 11. 디렉토리의 모든 gzip 파일의 바이트 수를 계산하십시오.
- 12. Python을 사용하여 디렉토리의 모든 파일에 대해 XML 태그 값을 파싱하십시오.
- 13. 디렉토리의 모든 파일에 perl 스크립트를 적용하고 Python을 사용하여 출력을 얻습니다.
- 14. GIT 저장소에서 특정 파일 형식의 평균 코드 행을 계산하십시오.
- 15. Python을 사용하여 QR 코드 조각을 확인하십시오.
- 16. Python을 사용하여 유니 코드 파일 검색
- 17. Python을 사용하여 원격 MySql DB에 코드 작성
- 18. python을 사용하여 cmd에 코드 작성 3.2
- 19. Python을 사용하여 코드 블록에서 href ID 추출하기
- 20. 특정 열에 대한 행을 계산하십시오.
- 21. PDF 암호화 Python을 사용하여
- 22. Python을 사용하여 SQLite에서 유니 코드 문자열을 올바르게 정렬하는 방법이 있습니까?
- 23. nltk 및 python을 사용하는 코드
- 24. SQL 행을 업데이트하면 Python을 사용하여 DB에 새로운 행이 추가됩니다.
- 25. Python을 사용하여 Google 퓨전 테이블에 행을 삽입하는 방법
- 26. Python을 사용하여 메모리에있는 파일 객체
- 27. python을 사용하여 단어에 쓰기
- 28. SQL에서 병합 결과의 행을 병합하고 계산하십시오.
- 29. mysql 쿼리에서 두 개의 다른 행을 계산하십시오.
- 30. TimeSpan을 사용하여 시차를 계산하십시오.
사용 ['''os.walk'''] (https://docs.python.org/3/library/os.html#os.walk) 파일과 하위 디렉토리를 트래버스하려면 ['''endswith'''] (https://docs.python.org/3/library/std)를 사용하십시오. types.html # str.endswith)를 사용하여 계산하려는 파일을 필터링하고 각 파일을 열어''''sum (1 for line in f)'''명령을 사용하여 모든 파일을 집계하고 파일 줄 수. – wwii