简体   繁体   English

如何将每日 .csv 写入特定文件路径

[英]How to write a daily .csv to a specific filepath

I'm running a program daily and want the .csv is generates to be written to a folder on my C drive.我每天都在运行一个程序,并希望将生成的 .csv 写入我的 C 驱动器上的文件夹中。 For some reason, I can create the folder and write 1 file but no others are being written.出于某种原因,我可以创建文件夹并写入 1 个文件,但没有写入其他文件。 Not getting any errors, just no other files are being written to that folder.没有收到任何错误,只是没有其他文件被写入该文件夹。 Here's the code.这是代码。 Thanks谢谢

Code:代码:

CSVdir = r"C:\Users\Maurice\Desktop\Python\New_Project\OptionsData\\OptionsData-{}.csv"
realCSVdir = os.path.realpath(CSVdir)

if not os.path.exists(CSVdir): 
    os.makedirs(CSVdir)
    str1 = "\n".join(data)
    now = datetime.datetime.now() #+ datetime.timedelta(days=1)
    now_str = now.strftime("%Y-%m-%d")

    new_file_name = os.path.join(realCSVdir,'OptionsData-{}.csv'.format(now_str)) 
    new_file = open(new_file_name, 'wb')

    for item in money_list:
        if len(item) != 0 :
            for other_item in item :
                new_file.write(other_item + str1 + new_file)

                new_file.close()

            print("Eureka!")
CSVdir = r"C:\Users\Maurice\Desktop\Python\New_Project\OptionsData\\OptionsData-{}.csv"

should be应该

CSVdir = r"C:\Users\Maurice\Desktop\Python\New_Project\OptionsData"

if not os.path.exists(CSVdir): 
    os.makedirs(CSVdir)

# The following lines should be out of if statement.

str1 = "\n".join(data)
now = datetime.datetime.now() #+ datetime.timedelta(days=1)
now_str = now.strftime("%Y-%m-%d")

new_file_name = os.path.join(realCSVdir,'OptionsData-{}.csv'.format(now_str)) 
new_file = open(new_file_name, 'wb')


for item in money_list:
    if len(item) != 0 :
        for other_item in item :
            new_file.write(other_item + str1 + new_file)

            new_file.close()

        print("Eureka!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM