0

我正在尝试编写 csv 文件并在每次用户使用它时创建一个新文件,而不是替换旧文件。此外,用户需要在开头输入他们的名字,所以我需要文件名是用户的名字。这是我的代码:

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

但是,有一个错误“with open(direction, cvs_file_name+'_results.csv', 'w') as resultFile: TypeError: an integer is required (got type str)”。

非常感谢您的帮助!

4

1 回答 1

0

第一个参数open是文件的整个路径,包括目录。尝试

with open(direction + cvs_file_name + '_results.csv', 'w') as resultFile:
    etc
于 2017-02-12T20:39:29.103 回答