files.Dir – File manipulation

class files.Dir([path])

The Dir class provides functions for common opperations on directories, such as moving, deleting, and traversing a directory.

If path is passed, the Dir object represents the object at that path. path may be a string or a Path object. Otherwise, a new temporary directory is created.

Dir.access([mode])
Dir.last_access()
Dir.last_change()
Dir.ctime()
Dir.move(new)
Dir.rename(new)
Dir.chown([uid, gid])
Dir.chmod([user="", group="", other="", *extra])
Dir.chflags(*flags)
All of these methods are the same as those of the File class. Please consult that documentation for a description.
Dir.files()
Return a list of Path objects representing files or directories in the current directory. Use [o.get() for o in dir.files()] to get the File or Dir objects in dir. The returned paths will be sorted alphabetically with directories first.
Dir.create([mode=0o777])
Creates the current directory. If possible, give it permissions as given in mode. This will create any parent directories as necessary.
Dir.delete()
Delete the current directory and all files and directories in it. Careful!
Dir.copy(dest[, symlinks=True])
Copy all files in the current directory to dest, which can be a string, Path, or Dir. If symlinks is False, symbolic links will not be followed.
Dir.walk(top[, topdown=True, onerror=None, followlinks=False])
Consult the :meth:os.walk documentation.
Dir.name
Same as File.name. Consult the File documentation.

Dir objects can be compared with < and > to sort them alphabetically. They can also be compared with File objects (and they will always be greater).

Dir objects can be used as the subject of a for loop, such as:

for i in d:
    print(i.get().name)

Previous topic

files.File – File manipulation

This Page

Quick search