0

我有这个函数与 python 创建一个简单的 CSV outFile 但我希望我选择使用 Windows 资源管理器保存的目录,我的函数:

def exporter():
    name_of_file="export"
    l = [[1, 2], [2, 3], [4, 5]]
    completeName = os.path.abspath("C:\temp\%s.csv" % name_of_file)
    out = open(completeName,"w")
    for row in l:
        for column in row:
            out.write('%d;' % column)
            out.write('\n')
        out.close()

    QObject.connect(export, SIGNAL('clicked()'),exporter)

export 是一个 QPushButton ,谢谢!

4

1 回答 1

0
def exporter(directory='C:\temp\\'):
    name_of_file = "export"
    l = [[1, 2], [2, 3], [4, 5]]
    completeName = os.path.abspath("C:/temp/%s.csv" % name_of_file)
    full_path = '%(directory)s\%(name_of_file)s.csv' % locals()
    out = open(full_path, "w")
    for row in l:
        for column in row:
            out.write('%d;' % column)
            out.write('\n')
        out.close()

    QObject.connect(export, SIGNAL('clicked()'),exporter)

像这样的东西可以解决问题。只需将路径作为参数传递即可。

于 2013-07-31T09:21:13.017 回答