0

I am using Spyder and accessing the reddit API to get some data and write it to csv, when I print out the lines, everything works fine, but then the csv file just doesn't get created, I tried many things but nothing seems to work, and a very similar piece of code worked out fine and I got the csv file, so I have no idea what the problem is.

with open('finalplswork.csv', 'wb') as fl:
    writr = csv.writer(fl)   
    for subid in idsss:
        submission = r.get_submission(submission_id=subid)
        created_utc=submission.created_utc
        created_date_utc = datetime.fromtimestamp(created_utc)
        data=(subid, created_utc, created_date_utc)
        writr.writerow(data)
2
  • How are you running the file? Check the directory where this python file is located, not the directory you are running it from. Commented Jul 4, 2013 at 11:26
  • For me, I had a ":" in my output filename and it just died silently. Like no exceptions, no errors, everything ran just fine. But the file was simply not created. Took me a while to figure out lol. Commented Aug 16, 2021 at 12:49

2 Answers 2

3

Your CSV is created, but you are using a relative file path.

You'd have to print os.getcwd() to know where it is being created, or set the current working directory to where you expect your file to appear.

Best to use an absolute file path instead:

with open('C:/full/path/to/your/documents/folder/finalplswork.csv', 'wb') as fl:
Sign up to request clarification or add additional context in comments.

3 Comments

Good theory. But one note: you do know where it is created: os.getcwd(). :)
@JohnZwinck: Sure, but I strongly suspect that the CWD is somewhere in C:\Python27, not the best place to go spelunking for a lost CSV file..
Not to mention on Windows unless you are using an elevated prompt, you can't write to most directories in the main system drive (ie, the "C drive").
0

I turned your code into a self-contained example:

with open('finalplswork.csv', 'wb') as fl:
    writr = csv.writer(fl)
    for subid in range(3):
        submission = 'foo'
        created_utc = 130000
        created_date_utc = 20130704
        data=(subid, created_utc, created_date_utc)
        writr.writerow(data)

It works fine:

$ cat finalplswork.csv 
0,130000,20130704
1,130000,20130704
2,130000,20130704

So your problem is not with the code you've shown here.

2 Comments

Yes, true, I just tried it in python on terminal as opposed to the Spyder IDE, and it worked, so I guess it is a Spyder bug. I will still accept your answer soon, or should I delete the question?
Don't delete your question, it's fine. I'm not sure it's a Spyder bug--see Martijn's answer!

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.