1import os
2
3files = os.listdir('.')
4print(files)
5for file in files:
6 # do something
7
1from os import listdir
2from os.path import isfile, join
3onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
1# Basic syntax:
2import os
3os.path.dirname('/path/to/your/favorite/file.txt')
4--> '/path/to/your/favorite/'
1import os
2
3def get_filepaths(directory):
4 """
5 This function will generate the file names in a directory
6 tree by walking the tree either top-down or bottom-up. For each
7 directory in the tree rooted at directory top (including top itself),
8 it yields a 3-tuple (dirpath, dirnames, filenames).
9 """
10 file_paths = [] # List which will store all of the full filepaths.
11
12 # Walk the tree.
13 for root, directories, files in os.walk(directory):
14 for filename in files:
15 # Join the two strings in order to form the full filepath.
16 filepath = os.path.join(root, filename)
17 file_paths.append(filepath) # Add it to the list.
18
19 return file_paths # Self-explanatory.
20
21# Run the above function and store its results in a variable.
22full_file_paths = get_filepaths("/Users/johnny/Desktop/TEST")
1from os import listdir
2from os.path import isfile, join
3onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
4