2016-04-07 17 views
0

毎日プログラムを実行していて、.csvが生成されてCドライブのフォルダに書き込まれるようにします。何らかの理由で、私はフォルダを作成して1つのファイルを書き込むことができますが、他のファイルは書き込まれません。エラーは発生せず、他のファイルはそのフォルダに書き込まれません。ここにコードがあります。おかげ毎日.csvを特定のファイルパスに書き込む方法

はコード:

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!") 

答えて

2
CSVdir = r"C:\Users\Maurice\Desktop\Python\New_Project\OptionsData\\OptionsData-{}.csv" 

私はあなたの変更を行ったが、まだdoesent仕事

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!") 
+0

でなければなりません。フォルダを作成して今日のファイルを入れますが、明日(now = datetime.datetime.now()+ datetime.timedelta(days = 1))に日付を変更すると、次のファイルはフォルダに追加されません。 ... – RageAgainstheMachine

+0

これは、複数のエラーがあったことを意味します。 – Hun

+0

それは私がエラーを持っていると言っていない... – RageAgainstheMachine

関連する問題