10 examples of 'python open file in different directory' in Python

Every line of 'python open file in different directory' 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

20def open(file, mode=None, encoding=None):
21 if mode is None:
22 mode = 'r'
23
24 if '/' in file:
25 if 'w' or 'a' in mode:
26 path_dir = os.path.dirname(file)
27 if not os.path.isdir(path_dir):
28 os.makedirs(path_dir)
29
30 f = builtins.open(file, mode=mode, encoding=encoding)
31 return f
55def open_dir(d):
56 """From: http://stackoverflow.com/a/1795849/965332"""
57 if sys.platform == 'win32':
58 os.startfile(d)
59 elif sys.platform == 'darwin':
60 subprocess.Popen(['open', d])
61 else:
62 subprocess.Popen(['xdg-open', d])
140def open(name, mode='r'):
141 """
142 open the file
143 """
144 try:
145 return file(name, mode)
146 except:
147 overlay = os.path.abspath(getoverlay(name))
148 if not overlay:
149 raise OSError
150 try:
151 if not os.path.isdir(os.path.dirname(overlay)):
152 os.makedirs(os.path.dirname(overlay), mode=04775)
153 except IOError:
154 logger.warning('error creating dir %s', os.path.dirname(overlay))
155 raise IOError
156 try:
157 return file(overlay, mode)
158 except IOError:
159 logger.warning('error opening file %s', overlay)
160 raise IOError
249def myopen(filename, mode="r"):
250 if "w" in mode:
251 ensure_dir(filename)
252 return open(filename, mode)
26def open_file(file_path):
27 global file
28 file = open(file_path, 'wb')
29 reset()
16def openFile(self, dir, fileName): # opens file, read only
17 openFile = open(dir + "/" + fileName, "rb")
18 return openFile
433def _open_file(self, module_name):
434 module_json_cache = self.module_json_cache
435 if module_name in module_json_cache:
436 for file_name in module_json_cache[module_name]["files"]:
437 if os.path.isfile(file_name) and not file_name.endswith("-meta.xml"):
438 self.window.open_file(file_name)
81def open(filename, mode, *args):
82 mode = mode.replace('U', '')
83 return _open(filename, mode, *args, errors='ignore')
5def safe_open(root: str, file: str, mode: str, *args, **kwargs):
6 for s in FILTER_STRING:
7 file.replace(s, "")
8 return open(os.path.join(os.path.abspath(root), file), mode, *args, **kwargs)

Related snippets