如何在特定文件夹上创建一个空的 csv 文件? [英] How do I create an empty csv file on a specific folder?

查看:113
本文介绍了如何在特定文件夹上创建一个空的 csv 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对如何创建一个空的 csv 文件有疑问,稍后我可以在其中打开它以使用 python 保存一些数据.我该怎么做?谢谢

I had a doubt on how to create an empty csv file where I can open it later to save some data using python. How do I do it? Thanks

推荐答案

可以像创建任何其他文件一样创建空的 csv 文件 - 您只需对其执行任何操作.使用 bash,您可以执行 touch/path/to/my/file.csv.

An empty csv file can be created like any other file - you just have to perform any operation on it. With bash, you can do touch /path/to/my/file.csv.

请注意,您不必为要写入的 Python 创建一个空文件.如果您写入一个不存在的文件,Python 将自动执行此操作.实际上,从python内部创建一个空文件意味着打开它进行写入,但不向其中写入任何内容.

Note that you do not have to create an empty file for python to write in. Python will do so automatically if you write to a non-existing file. In fact, creating an empty file from within python means to open it for writing, but not writing anything to it.

with open("foo.csv", "w") as my_empty_csv:
  # now you have an empty file already
  pass  # or write something to it already

这篇关于如何在特定文件夹上创建一个空的 csv 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆