python csv write to a new additional file without replace old file

284 Views Asked by At

I am trying to write csv file and create a new file every time when a user using it, rather than replace the old file. Moreover, the users will need to enter their name at the beginning, so I need the file name to be the users' name. Here is my code:

    direction = "/Users/Desktop/"
    cvs_file_name = str(self.entrySub.get()) ###'entrySub' is where users enter the name, and I use .get() to retrieve it.### 
    with open(direction, cvs_file_name+'_results.csv', 'w') as resultFile:
        resultFileWrite = csv.writer(resultFile, quoting=csv.QUOTE_ALL)
        resultFileWrite.writerow(['Subject', "Session"])
        resultFileWrite.writerow([self.entrySub.get(), self.entrySes.get()])
        resultFile.flush()

However, there's an error "with open(direction, cvs_file_name+'_results.csv', 'w') as resultFile: TypeError: an integer is required (got type str)".

Really appreciate for your help!

1

There are 1 best solutions below

0
On

The first parameter to open is the whole path of the file, including the directory. Try

with open(direction + cvs_file_name + '_results.csv', 'w') as resultFile:
    etc