파이썬 glob 패키지를 사용하여 디렉토리의 모든 파일 이름을 선택하는 프로그램을 만들었습니다.
import glob
path = 'D:\Movies\American and Brit Cinema'
files = [f for f in glob.glob(path + "**/*.avi", recursive=True)]
for f in files:
print(f[35:])
원하는 출력을 얻습니다.
The.Descendants.2011.
The.Expendables.2010.
The.Hangover.2009
The.Impossible.2012
the.invention.of.lying.2009
The.Muppets.2011.
The.Social.Network.2010
Tron.Legacy.2010
그러나이 영화 제목 목록을 CSV로 내보내고 싶습니다.
시도 할 때 아래 오류가 발생합니다.
f.to_csv('EnglishMovies.docx')
AttributeError: 'str' object has no attribute 'to_csv'
나는 또한 목록으로 변환하려고 시도했지만 지저분한 데이터를 얻습니다. 최종 csv 파일에 디렉토리의 모든 파일 이름이 있기를 원합니다.
csv
모듈 사용
import csv
with open('output.csv', mode='w') as csvfile:
writer = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
for f in files:
writer.writerow([f])
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다