Skip to content

Folder

Folder module for autopylot. This module contains functions for working with folders and files.

Examples:

>>> from autopylot import folder
>>> folder.create_folder(path='tests\demo')
>>> folder.delete_folder(path='tests\demo')

It contains the following functions: - is_folder(path): Returns True if the given path is a folder. - create_folder(path): Create a folder at the given path. - delete_folder(path): Delete a folder at the given path. - rename_folder(path, new_name): Rename a folder at the given path. - copy_folder(source, destination): Copy a folder from the source to the destination. - move_folder(source, destination): Move a folder from the source to the destination. - get_size(path) -> int: Get the size of a folder in bytes. - get_size_human(path) -> str: Get the size of a folder in human readable format. - get_contents(path) -> list: Get a list of all files and folders in a folder. - get_contents_recursive(path) -> list: Get a list of all files and folders in a folder and all subfolders.

copy_folder(source, destination, only_contents=False)

Copy a folder from the source to the destination.

Parameters:

Name Type Description Default
source str || WindowsPath

The path to the source folder.

required
destination str || WindowsPath

The path to the destination folder.

required
only_contents bool

If True, only the contents of the folder will be copied. Default is False.

False

Returns:

Type Description
None

None

Examples:

>>> folder.copy_folder(source='tests\demo',destination= 'tests\demo2')
>>> os.path.exists('tests\demo2')
True

create_folder(path)

Create a folder at the given path.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required

Returns:

Type Description
None

None

Examples:

>>> folder.create_folder(path='tests\demo')
>>> os.path.exists('tests\demo')
True

delete_folder(path)

Delete a folder at the given path.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required

Returns:

Type Description
None

None

Examples:

>>> folder.delete_folder(path='tests\demo')
>>> os.path.exists('tests\demo')
False

get_contents(path, extension='all')

Get a list of all files and folders in a folder.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required
extension str

The extension of the files to return. Default is "all".

'all'

Returns:

Name Type Description
contents list

A list of all files and folders in the folder.

Examples:

>>> folder.get_contents(path='tests')
['demo', 'demo2', 'demo.txt']

get_contents_recursive(path, extension='all')

Get a list of all files and folders in a folder and all subfolders.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required
extension str

The extension of the files to return. Default is "all".

'all'

Returns:

Name Type Description
contents list

A list of all files and folders in the folder and all subfolders.

Examples:

>>> folder.get_contents_recursive(path='tests')
['demo', 'demo2', 'demo.txt']

get_size(path)

Get the size of a folder in bytes.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required

Returns:

Name Type Description
size int

The size of the folder in bytes.

Examples:

>>> folder.get_size(path='tests')
0

get_size_human(path)

Get the size of a folder in human readable format.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required

Returns:

Name Type Description
size str

The size of the folder in human readable format.

Examples:

>>> folder.get_size_human(path='tests')
'0 bytes'

is_folder(path)

Returns True if the given path is a folder.

Parameters:

Name Type Description Default
path str || Windows Path

Path of the folder.

required

Returns:

Type Description
bool

Is Folder (bool): True if the given path is a folder, False otherwise.

Examples:

>>> is_folder(path='C:\Users\abc\Desktop\folder')

move_folder(source, destination)

Move a folder from the source to the destination.

Parameters:

Name Type Description Default
source str || WindowsPath

The path to the source folder.

required
destination str || WindowsPath

The path to the destination folder.

required

Returns:

Type Description
None

None

Examples:

>>> folder.move_folder(source='tests\demo', destination='tests\demo2')
>>> os.path.exists('tests\demo')
False
>>> os.path.exists('tests\demo2')
True

rename_folder(path, new_name)

Rename a folder at the given path.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the folder.

required
new_name str

The new name of the folder.

required

Returns:

Type Description
None

None

Examples:

>>> folder.rename_folder(path='tests\demo', new_name='demo2')
>>> os.path.exists('tests\demo2')
True