python

파일 다중 만들기

bumychoi 2024. 7. 5. 20:00
file_list=["A","B","C","D","E","F"]
content_list=["Python","javaScript","PHP","Rust","Solidity","Assembly"]
content_list2=[ ["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"],
                ]
import os

#방법1
def write_contents1(filepath):

    if not os.path.exists(filepath):
        os.makedirs(filepath)
    # loop write
    for n,c in zip (file_list,content_list):
        with open(filepath+n+".txt","w") as file:
            file.write(c)

def write_contents2(filepath):

    if not os.path.exists(filepath):
        os.makedirs(filepath)
    # loop write
    for n,c in zip (file_list,content_list2):
        with open(filepath+n+".txt","w") as file:
            file.writelines(c+"\n" for c in c)

write_contents1("ex//26-1/")
write_contents2("ex//26-2/")