0

The purpose of the code is to put the output into a text file the problem i have is it says i have a syntax error and would like to know what i am missing

def save():
with open(os.path.join('C:\Users\test\Documents\test', 'directory.txt','w+'))#syntax error in this line
directoryScan.write(outPut) #file
directoryScan.close()

the w+ is used for - if the file does not exist create a .txt file and insert the output there

1 Answer 1

1

Go through Section 7.2 here, Python Input output Documentation

Your with is incomplete, hence the syntax issue.
Also the format for opening should be open('file','w+') and in your case 'file' is replaced with os.path.join(), but you seem to be treating it as open(os.path.join()).

When you are done, your code should be something like :
with open (os.path.join(<path>,<filename>), 'w+') as directoryScan:

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

2 Comments

Thank you for the link it was useful
Glad! notice that you don't have to close the filestream when using with operator. It closes it by default.

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.