createfile - I don't get why my python file creation is not working -


import sys  filepath = "d:\\desktop\\sign in out\\" times = ["min.txt","hour.txt","datein.txt","dateout.txt"]  while true:     z = input("enter first , last name. don't put . in name. : ")     y = z+'\\'+z     x in range(len(times)):         f = open(filepath+y+times[x],'w')         f.write('0')         f.close() 

joining files \\ not best way it. recommended way using os.path.join os module:

import os  while true:     z = input("enter first , last name. don't put . in name. : ")     y = os.path.join(*z.split())     x in times:         open(os.path.join(filepath, y, x), 'w') f:             f.write('0') 

i recommend -

  1. using context manager handle files, makes code cleaner.

  2. iterating on times directly rather indices


also, jean-françois fabre states in answer, you'd better use os.mkdir create directories don't exist before create files in them. here's reference.


Comments

Popular posts from this blog

Ansible warning on jinja2 braces on when -

Parsing a protocol message from Go by Java -

html - How to custom Bootstrap grid height? -