7 examples of 'python open file' in Python

Every line of 'python open file' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
17def open_python_file(filename):
18 """Open a read-only Python file taking proper care of its encoding.
19
20 In Python 3, we would like all files to be opened with utf-8 encoding.
21 However, some author like to specify PEP263 headers in their source files
22 with their own encodings. In that case, we should respect the author's
23 encoding.
24 """
25 import tokenize
26
27 if hasattr(tokenize, "open"): # Added in Python 3.2
28 # Open file respecting PEP263 encoding. If no encoding header is
29 # found, opens as utf-8.
30 return tokenize.open(filename)
31 else:
32 return open(filename, "r")
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

26def open_file(file_path):
27 global file
28 file = open(file_path, 'wb')
29 reset()
212def open(self, event=None):
213 '''
214 Open given file
215 '''
216 with open(self.file.path, encoding='UTF-8') as file:
217 file_text = file.read()
218 if file_text:
219 self.modified_event_triggered_by_opening_file = True
220 self.insert(tk.END, file_text)
81def open(filename, mode, *args):
82 mode = mode.replace('U', '')
83 return _open(filename, mode, *args, errors='ignore')
27def _open_file(filename):
28 if sys.platform == "win32":
29 os.startfile(filename)
30 else:
31 if sys.platform == "darwin":
32 opener = "open"
33 else:
34 opener = "xdg-open"
35 subprocess.call([opener, filename])
99def file_open(*args, **kwargs):
100 """Open file
101
102 see built-in open() documentation for more details
103
104 Note: The reason this is kept in a separate module is to easily
105 be able to provide a stub module that doesn't alter system
106 state at all (for unit tests)
107 """
108 return open(*args, **kwargs)
27def open_file(name, mode, **kwargs):
28 r = {
29 "w": SpageWriter,
30 "r": {"spage": SpageReader, "offpage": OffpageReader, "s2o": SpageToOffpage},
31 }.get(mode, __not_supported_mode)
32 if mode == "r":
33 r = r.get(kwargs.pop("page_type", "spage"), __not_supported_page_type)
34
35 return r(name, **kwargs)

Related snippets