17

I want to know how to create a file if it does not exist in the directory. I want to only append data.

I am getting this error in Python: No such file or directory.

This is my code:

with open (saveAddr+".csv",'a') as allpckts:                            
    writer = csv.DictWriter(allpckts, delimiter=',', fieldnames=header) 
    if pktnum < 2:                                                        
        writer.writerow(dict((fn,fn) for fn in header))                 
        writer.writerow(packet_data)                                    
    else:                                                               
        writer.writerow(packet_data)    

Update: My problem was that I wasn't in right directory. So for anyone searching for the most basic syntax to only append to CSV file is:

with open (filename+".csv",'a') as filedata:                            
    writer = csv.DictWriter(filedata, delimiter=',', fieldnames=header)
    writer.writerow(data) 
alphiii
  • 1,597
  • 3
  • 21
  • 27
  • What is saveAddr ? What you want is what 'a' mode does . – Anand S Kumar Aug 19 '15 at 06:15
  • saveAddr is name of the file that I want to create if it does not exist. I my case it is smth. like ED_C0_B0_E0_D2_87 – alphiii Aug 19 '15 at 06:16
  • Does this work by itself? `open(saveAddr+".csv", 'a')` – Paul Aug 19 '15 at 06:16
  • 1
    Please show the full error message, not just a snippet of it. It could be that a directory name in the path specified does not exist. – cdarke Aug 19 '15 at 06:16
  • May be this information helps you: http://stackoverflow.com/questions/13248020/whats-the-difference-between-r-and-a-when-open-file-in-python – ikoverdyaev Aug 19 '15 at 06:17
  • The problem wasnt the this code but it was two lines upper since I wanted to read a file that does not existI just put this code to solve the issue. try: file_read = open(saveAddr+".csv") pktnum = sum(1 for row in file_read) except: pktnum = 0 – alphiii Aug 19 '15 at 06:27

3 Answers3

9

Most probably you are trying to create a file in a directory which does not exist .

What you want is what 'a' mode does , it creates the file if it does not exist , otherwise it appends to the file . But it would not create the directories , if those directories so not exist , you should create the directories used in saveAddr , before running the program .

If you want a programmatic solution , you can check out os.mkdir , which should create the directory.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
7
with open (saveAddr+".csv",'a') as allpckts:

will create a new file saveAddr+".csv" if not exist, otherwise open it for further appending.Assuming saveAddr is the file name(if path includes in it, check whether path exists.)

If you want to check file exists

os.path.isfile('/path/to/csv')
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49
4
#check if dir exist if not create it
def check_dir(file_name):
    directory = os.path.dirname(file_name)
    if not os.path.exists(directory):
        os.makedirs(directory)


def save(file_name, records):
    check_dir(file_name)
    csv_file = open(file_name,'w+')
    csvWriter = csv.writer(csv_file,delimiter=',')
    count = 0
    for record in records:
        csvWriter.writerow([record])
        count+=1

    print(count, " record saved to ",file_name)
    return  count    enter code here

directory = os.path.abspath(os.path.join(os.path.curdir))
save(directory+"/data/filename.csv",your_list)