0

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'

5
  • you can't close the file after the with statement (it is already closed, with finalizes by calling close on f, 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. Commented Nov 1, 2015 at 14:20
  • Thank you and in my code, I haven't specified the extension have I? Commented Nov 1, 2015 at 14:23
  • EncryptedMessages or Encrypted Messages ? Commented Nov 1, 2015 at 14:34
  • 1) you haven't provided the full error message always do that, 2) extensions are irrelevant for files. Commented Nov 1, 2015 at 14:38
  • I changed the path name as a test to EncryptedMessages Commented Nov 1, 2015 at 14:38

1 Answer 1

1

Below code is working for me-USE raw_input i am 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 the name of your file "))
fullpath = os.path.join(encpath, name)
new_file = open(fullpath, 'w')
with open(fullpath, 'a') as f:
    f.write(translated + '\n')
    f.close()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.