I have some code which is meant to: create a new directory; ask the user to enter some text to put in the file; create the file; join the file name and path together and then write the text from translated into the file. But when I run the code below I get
with open(new_file, 'a') as 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 your text"))
encpath = r'C:\Downloads\Encrypted Messages'
def make_sure_path_exists(encpath):
try:
os.makedirs(encpath)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
name = str(input("Please enter the name of your file "))
fullpath = os.path.join(encpath, name)
new_file = open(fullpath, 'w')
with open(new_file, 'a') as f:
f.write(translated + '\n')
I have also 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 the name of your file "))
fullpath = os.path.join(encpath, name)
new_file = open(fullpath, 'w')
with open(new_file, 'a') as f:
f.write(translated + '\n')
I am using Python 3.5.0 in case you're wondering.
EDIT: I've renamed encpath to r'C:\Downloads\EncryptedMessages' and I get a new error: FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Downloads\\EncryptedMessages\\justatest'
withfinalizes by calling close onf, I guess this is where the error comes in?). The first code does not create the folder, as you do not call the routine you create.EncryptedMessagesorEncrypted Messages?EncryptedMessages