3 examples of 'python open stdin' in Python

Every line of 'python open stdin' 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

29def __init__(self, original_stdin=sys.stdin, *args, **kwargs):
30 try:
31 self.encoding = sys.stdin.encoding
32 except:
33 # Not sure if it's available in all Python versions...
34 pass
35 self.original_stdin = original_stdin
36
37 try:
38 self.errors = sys.stdin.errors # Who knew? sys streams have an errors attribute!
39 except:
40 # Not sure if it's available in all Python versions...
41 pass
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)

Related snippets