1 Replies - 528 Views - Last Post: 22 August 2012 - 11:44 PM Rate Topic: -----

#1 shulgin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 12-August 12

File write issues, wont create file

Posted 22 August 2012 - 11:07 PM

Hey guys,

Am wanting to attach the date to the name of a text file that gets generated as a result of a whole heap of user inputs.

What I have done is save the name of the file as a variable called name. When I do this everything runs fine and I don't have any issues.

However when I try to attach a date (saved as a string) it comes up with this


Traceback (most recent call last):
File "datalogger.py", line 165, in <module>
f = open(name + ".txt", "w")
IOError: [Errno 2] No such file or directory: 'new23/8/2012.txt'

This is how I get the date


from datetime import datetime
now = datetime.now()
current_year = now.year
current_month = now.month
current_day = now.day

date = str(now.day) + "/" + str(now.month) + "/" + str(now.year)



this is how I get the file name and add the variable date to it


name = raw_input("What would you like to name yoru file ?: \n")
name = name + str(date)




and this is how I'm writing the file


f = open(name + ".txt", "w")
f.write(users inputs here) 




When I just keep the name as the user defines all works fine but when I add the variable date to it the file wont write.

Any help is appreciated

Cheers

Aaron

Is This A Good Question/Topic? 0
  • +

Replies To: File write issues, wont create file

#2 shulgin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 12-August 12

Re: File write issues, wont create file

Posted 22 August 2012 - 11:44 PM

Solved the problem,

For those wanting to know how I think the "/" were causing the programe to try and find a separate directory as opposed to writing in the directory where the .py file is. I simply replaced the "/" with "-" and all works fine !!!

Cheers anyway

Aaron
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1