【问题标题】:How do you write CSV files to a specific folder that you've recently created in python?如何将 CSV 文件写入您最近在 python 中创建的特定文件夹?
【发布时间】:2015-01-12 04:50:42
【问题描述】:

这是我目前拥有的:

CSVdir = "./CSV_folder"
realCSVdir = os.path.realpath(CSVdir)
if not os.path.exists(CSVdir):
    os.makedirs(CSVdir)
writer = csv.writer(open(realCSVdir + 'Movie-%s.csv' % var, 'wb'))

我正在尝试将我的 CSV 文件写入刚刚创建的 CSVdir,但它不起作用。

【问题讨论】:

  • 这有什么问题?你有追溯吗?

标签: python csv directory


【解决方案1】:

问题是您遗漏了realCSVdir 和文件名之间的路径分隔符。

os.path.join 用于跨平台解决方案:

# this is meant to follow on from the `os.makedirs` line...
foupath = os.path.join(realCSVdir, 'Movie-%s.csv' % var)
fou = open(foupath, 'wb')
writer = csv.writer(fou)

【讨论】:

    猜你喜欢
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    相关资源
    最近更新 更多