11

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

2 Answers 2

30

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.

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
Sign up to request clarification or add additional context in comments.

Comments

3

you can also use Pandas to do the same as below:

import pandas as pd
df = pd.DataFrame(list())
df.to_csv('empty_csv.csv')

After creating above file you can Edit exported file as per your requirement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.