사이트에서 파일을 다운로드하고 원본 파일을 저장 한 다음 파일을 다운로드 한 url과 다운로드 날짜를 파일을 다른 디렉토리에 저장하십시오.파이썬 csv : 다른 디렉토리에 쓰기
나는 CSV 개정이 답변을 사용했습니다 : how to Add New column in beginning of CSV file by Python
을하지만 쓰기() 함수가 호출되기 전에 다른 디렉토리에 파일을 리디렉션 할 사투를 벌인거야.
파일을 쓰고 이동 시키거나, open() 함수 내에서 다른 디렉토리에 파일을 쓰는 방법이 있습니까?
if fileName in fileList:
print "already got file "+ fileName
else:
# download the file
urllib.urlretrieve(csvUrl, os.path.basename(fileName))
#print "Saving to 1_Downloaded "+ fileName
# open the file and then add the extra columns
with open(fileName, 'rb') as inf, open("out_"+fileName, 'wb') as outf:
csvreader = csv.DictReader(inf)
# add column names to beginning
fieldnames = ['url_source','downloaded_at'] + csvreader.fieldnames
csvwriter = csv.DictWriter(outf, fieldnames)
csvwriter.writeheader()
for node, row in enumerate(csvreader, 1):
csvwriter.writerow(dict(row, url_source=csvUrl, downloaded_at=today))
당신이 새로운 밖으로 파일을 작성 : –
예, 새로운 출력 파일을 만들어야합니다. – woodbine
왜 열 때 파일을 저장할 디렉토리에 파일을 만들지 않으십니까? –