File
File module for autopylot. This module is used to interact with files.
Examples:
>>> from autopylot import file
>>> file.read_text(path='tests\demo.txt')
'This is a demo text file.'
It contains the following functions:
is_file(path): Returns True if the given path is a file.read_text(path): Read a text file and return the content as a string.write_text(path, contents): Write a text file with the given content.copy(source, destination): Copy a file from the source to the destination.move(source, destination): Move a file from the source to the destination.delete(path): Delete a file at the given path.rename(path, new_name): Rename a file at the given path.create(path): Create a file at the given path.get_json_content(path): Get the contents of a json file as a dictionary.get_json_section(path, section): Get a section from a json file and return it.set_json_section(path, section, value): Set a section in a json file.
copy(source, destination)
Copy a file from source to destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source |
str || WindowsPath
|
The path to the source file. |
required |
destination |
str || WindowsPath
|
The path to the destination file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
create(path)
delete(path)
get_json_content(path)
get_json_section(path, section)
Returns the details of the given section in the json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str || Windows Path
|
Path of the json file. |
required |
section |
str
|
Section of the json file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
data |
any
|
Details of the given section in the json file. |
Examples:
is_file(path)
move(source, destination)
Move a file from source to destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source |
str || WindowsPath
|
The path to the source file. |
required |
destination |
str || WindowsPath
|
The path to the destination file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
read_text(path)
Reads a text file and returns its contents as a string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str || list || WindowsPath
|
The path to the text file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
content |
str || list
|
The contents of the text file, If a list of paths is provided, a list of strings is returned. |
Examples:
rename(path, new_name)
Rename a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str || WindowsPath
|
The path to the file. |
required |
new_name |
str
|
The new name of the file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
set_json_section(path, section, value)
Sets the details of the given section in the json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str || Windows Path
|
Path of the json file. |
required |
section |
str
|
Section of the json file. |
required |
value |
any
|
Details of the given section in the json file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples:
write_text(path, contents)
Write a text file with the given contents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path |
str || WindowsPath
|
The path to the text file. |
required |
contents |
str
|
The contents of the text file. |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Examples: