如何让选择目录来保存python中的csv文件?

时间:2013-07-31 09:10:18

标签: python csv

我有这个函数与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)

导出是QPushButton,谢谢!

1 个答案:

答案 0 :(得分: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)

这样的事情可以解决问题。只需将路径作为参数传递。