In order to open a file in Python using with statement, you need to use the syntax: with open('example. txt', 'w') as file: file. write('Hello World!'). After Python opens the file, it assigns a file pointer to the variable MyFile. Therefore, you can use read and write operations using MyFile object. Remember to check for syntax errors.