python - Getting an error when trying to create/check for a new directory -
i have code meant to: create new directory; ask user enter text put in file; create file; join file name , path , write text translated
file. when run code below with open(new_file, 'a') f: typeerror: invalid file: <_io.textiowrapper name='c:\\downloads\\encrypted messages\\hi' mode='w' encoding='cp1252'>
import os import os.path import errno translated = str(input("please enter text")) encpath = r'c:\downloads\encrypted messages' def make_sure_path_exists(encpath): try: os.makedirs(encpath) except oserror exception: if exception.errno != errno.eexist: raise name = str(input("please enter name of file ")) fullpath = os.path.join(encpath, name) new_file = open(fullpath, 'w') open(new_file, 'a') f: f.write(translated + '\n')
i have tried
import os import os.path import errno translated = "hello world" encpath = r'c:\downloads\encrypted messages' if not os.path.exists(encpath): os.makedirs(encpath) name = str(input("please enter name of file ")) fullpath = os.path.join(encpath, name) new_file = open(fullpath, 'w') open(new_file, 'a') f: f.write(translated + '\n')
i using python 3.5.0 in case you're wondering.
edit: i've renamed encpath
r'c:\downloads\encryptedmessages'
, new error: filenotfounderror: [errno 2] no such file or directory: 'c:\\downloads\\encryptedmessages\\justatest'
below code working me-use raw_input
in 2.7
import os import os.path import errno translated = "hello world" encpath = r'c:\downloads\encrypted messages' if not os.path.exists(encpath): os.makedirs(encpath) name = str(raw_input("please enter name of file ")) fullpath = os.path.join(encpath, name) new_file = open(fullpath, 'w') open(fullpath, 'a') f: f.write(translated + '\n') f.close()
Comments
Post a Comment