6 examples of 'open a directory in python' in Python

Every line of 'open a directory in python' 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
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])
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

8def open(name):
9 print 'opening', name, '...'
10 if os.path.isdir(name):
11 w = dirwin.open(name)
12 else:
13 w = filewin.open(name)
14 return w
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
1181def loadDir(self, path):
1182 """ Create a project with the dir's name and add all files
1183 contained in the directory to it.
1184 extensions is a komma separated list of extenstions of files
1185 to accept...
1186 """
1187
1188 # if the path does not exist, stop
1189 path = os.path.abspath(path)
1190 if not os.path.isdir(path):
1191 print("ERROR loading dir: the specified directory does not exist!")
1192 return
1193
1194 # get extensions
1195 extensions = pyzo.config.advanced.fileExtensionsToLoadFromDir
1196 extensions = extensions.replace(',',' ').replace(';',' ')
1197 extensions = ["."+a.lstrip(".").strip() for a in extensions.split(" ")]
1198
1199 # init item
1200 item = None
1201
1202 # open all qualified files...
1203 self._tabs.setUpdatesEnabled(False)
1204 try:
1205 filelist = os.listdir(path)
1206 for filename in filelist:
1207 filename = os.path.join(path, filename)
1208 ext = os.path.splitext(filename)[1]
1209 if str(ext) in extensions:
1210 item = self.loadFile(filename, False)
1211 finally:
1212 self._tabs.setUpdatesEnabled(True)
1213 self._tabs.updateItems()
1214
1215 # return lastopened item
1216 return item
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
98def openFolder(self):
99 import os, sys
100 fname = os.path.abspath(self.lpystudio.simulations[self.selection].fname)
101 mdir = os.path.dirname(fname)
102 if sys.platform == 'win32':
103 import subprocess
104 subprocess.call('explorer /select,"'+fname+'"')
105 elif sys.platform == 'linux2':
106 os.system('xdg-open "'+mdir+'"')
107 else:
108 os.system('open "'+mdir+'"')

Related snippets