[Python]여러 파일에 저장
파이썬에서 여러개의 파일을 동시에 저장하는 방법을 알아보자.
import os filenames = ["A", "B", "C", "D", "F", "G"] contents = ["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"] contents2 = [["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"], ["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"], ["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"], ["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"], ["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"], ["Python", "JavaScript", "PHP", "Rust", "Solidity", "Assembly"]] # 방법1 def write_file(path, filename, content): if os.path.exists(path) == False: os.makedirs(path) with open(path+filename, 'w') as file: file.write(content) for filename, content in zip(filenames, contents): # print(filename, content) write_file("../source/26-1/", f"{filename}.txt", content) # 방법2 def write_file2(path, filename, content): if os.path.exists(path) == False: os.makedirs(path) with open(path+filename, 'w') as file: file.writelines(c + '\n' for c in content) for filename, content in zip(filenames, contents2): # print(filename, content) write_file2("../source/26-2/", f"{filename}.txt", content)
최신 댓글