Python Working with Directories Reference

My notes compiled on the topic of working with directories, navigating folders and listing files etc.

#Use os.listdir (or os.scandir in >= 3.5) to list items in a single directory, use os.walk (which uses os.listdir or os.scandir in >= 3.5) to list items in a directory tree.

#Using os.walk for only one directory is not as efficient as just using the function it uses internally to do that job.

#os.listdir(path='.')

#os.listdir returns a list of names (files and directories) in a given path

#default value for path when you call os.listdir() is '.'

#so os.listdir() and os.listdir('.') do the same thing, grabbing the current directory

#path can be specified as either str or bytes. If path is bytes, the filenames returned will also be bytes; in all other circumstances the filenames returned will be str.

#What does it mean when the docs say that os.listdir specifically excludes '.' and '..' even if they are present in the directory?

#That concerns the returned values. In UNIX filesystems, every directory has . and .. entries, where . refers to the current directory, and .. to the parent directory. These entries will not be included in the list returned by os.listdir.

#The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.

#glob.glob is a wrapper around listdir

import os

#os.getcwd()
#os.chdir("subFolder")
#os.mkdir("My New Folder")
#os.rename("My New Folder", "My New Folder renamed")

#The listdir function takes a pathname and returns a list of the contents of the directory.
print(os.listdir("c:\\Files\\"))

# another way
myfiles = os.listdir("c:\\Files\\")
print(myfiles)

#same thing but using variables.
dirname = "c:\\Files\\"
print(os.listdir(dirname))

#all three list the current directory
print(os.listdir())
print(os.listdir("."))
print(os.listdir("./"))

# print contents of a sub folder in current directory
print(os.listdir("subFolder"), "\n")

#output a list
print(os.listdir("."))

#loop over each item in the list
for name in os.listdir():
    print(name)

#or place in variable first
filelist = os.listdir()

#just print the variable contents
print(filelist)

#loop over each item in the variable's list
for name in filelist:
    print(name)

#its common to use os.path.join to get full paths
for entry in os.listdir("."):
    path = os.path.join(".", entry)
    print(path)

#use os.path.isdir() to filter out directories, printing only files
for name in filelist:
    if os.path.isdir(name):
        continue
    print(name)

#os.path.isfile() is an alternative, testing for 'fileness' instead

#so alternatively...

#only files
for x in os.listdir("."):
    if os.path.isfile(x):
        print(x)
#or

#only directories
for x in os.listdir("."):
    if os.path.isdir(x):
        print(x)

#a list comprehension using os.path.isfile() to return list of filenames only:
files = [f for f in os.listdir(".") if os.path.isfile(os.path.join(".", f))]
for file in files:
    print(file)

#another alternative using filter function
files = filter(os.path.isfile, os.listdir(os.curdir))

print(type(files)) #<class 'filter'>

print(files) #<filter object at 0x032C1BD0>

#loop over the filter object
for file in files:
    print(file)

#filter filetypes by using endswith()
for file in os.listdir("./"):
    if file.endswith(".jpg"):
        print(file)

#using fnmatch we can filter files by type
import fnmatch
listOfFiles = os.listdir('.')  
pattern = "*.py"  
for entry in listOfFiles:  
    if fnmatch.fnmatch(entry, pattern):
            print(entry)