7 examples of 'python sort files by name' in Python

Every line of 'python sort files by name' 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
250def compare(x, y):
251 if x == y:
252 return 0
253 if os.path.splitext(y)[0] in ['index', 'README']:
254 return 1
255 if os.path.splitext(x)[0] in ['index', 'README'] or x < y:
256 return -1
257 return 1
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

58def sort_listdir(dir):
59 list = os.listdir(dir)
60 list.sort()
61 return [os.path.join(dir,file) for file in list]
62def sort_by_file(self):
63 """ sort references by file """
64
65 self.refs_by_file = {} # key is the fn2k'ed filename
66
67 for ref in self.refs:
68 file_ = fn2k(ref['file'])
69 if file_ not in self.refs_by_file:
70 self.refs_by_file[file_] = []
71
72 self.refs_by_file[file_].append(ref)
299def sort_key(name):
300 if name.startswith('external.'):
301 return '0.' + name
302 elif name.startswith('integration.current.'):
303 return name.replace('integration.current.', '1.integration.0.current.')
304 elif name.startswith('integration.'):
305 return name.replace('integration.', '1.integration.')
306 return name
67def file_sort(x, y):
68 x1 = _get_index(x)
69 y1 = _get_index(y)
70 if x1 > y1:
71 return 1
72 elif y1 > x1:
73 return -1
74 else:
75 return 0
269def _visit_pyfiles(list, dirname, names):
270 """ Helper for getFilesForName().
271 """
272 # get extension for python source files
273 if "_py_ext" not in globals():
274 import imp
275
276 global _py_ext
277 _py_ext = [
278 triple[0] for triple in imp.get_suffixes() if triple[2] == imp.PY_SOURCE
279 ][0]
280
281 # don't recurse into CVS directories
282 if "CVS" in names:
283 names.remove("CVS")
284
285 # add all *.py files to list
286 list.extend(
287 [
288 os.path.join(dirname, file)
289 for file in names
290 if os.path.splitext(file)[1] == _py_ext
291 ]
292 )
147def sort_dircount_then_filename(item1, item2):
148 # First Compare dirpaths
149 if item1.dircount != item2.dircount:
150 return cmp(item1.dircount, item2.dircount)
151
152 # Next compare filenames
153 next_result = cmp(item1.filename, item2.filename)
154 if next_result != 0:
155 return next_result
156
157 # Lastly, compare filecounts
158 return cmp(item1.filecount, item2.filecount)

Related snippets