18

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) 
6
  • What is saveAddr ? What you want is what 'a' mode does . Commented Aug 19, 2015 at 6: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 Commented Aug 19, 2015 at 6:16
  • Does this work by itself? open(saveAddr+".csv", 'a') Commented Aug 19, 2015 at 6: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. Commented Aug 19, 2015 at 6:16
  • May be this information helps you: stackoverflow.com/questions/13248020/… Commented Aug 19, 2015 at 6:17

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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')

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.