1

The code that I am currently using is this:

date = time.strftime("%d/%m/%y")
filename = ('attendence{}' + str(date) +'.txt')
f = open(filename, 'w+')

However, the error I receive is this:

FileNotFoundError: [Errno 2] No such file or directory: 'attendence{}31/03/17.txt'

The error is not with other parts of my code as this will work.

f = open('attendence{}.txt', 'w+')

my end goal is to create a new file that contains the current date.

5
  • 1
    What do you want the name of the file to be? Commented Mar 31, 2017 at 19:21
  • I'm not sure what operating system you're on, but you generally can't use slashes in filenames; they'll be interpreted as directory separators. Commented Mar 31, 2017 at 19:22
  • attendance{}''datehere''.txt Commented Mar 31, 2017 at 19:22
  • You should use context management. with open(filename, 'w+') as f: ... Commented Mar 31, 2017 at 19:31
  • I would suggest removing the slashes, so date = time.strftime("%d%m%y") instead. Commented Mar 31, 2017 at 20:18

1 Answer 1

1

The problem is with the date format :

date = time.strftime("%d/%m/%y")

You could try :

date = time.strftime("%d_%m_%y")

'attendence{}31/03/17.txt' isn't just a filename, it's a relative path with :

  • 1 folder : 'attendence{}31'
  • 1 subfolder : '03'
  • 1 filename '17.txt'.

Python is complaining that the folder 'attendence{}31/03' doesn't exist.

Note that {} might confuse the system, some programs or some users. If there's no information inside the curly brackets, you might as well remove them.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the answer have changed the date to have . instead of / and it now works!

Your Answer

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