Skip to content

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:

>>> file.copy(source='tests\demo.txt', destination='tests\demo2.txt')
>>> file.read_text(path='tests\demo2.txt')
'This is a demo text file.'

create(path)

Create a file.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the file.

required

Returns:

Type Description
None

None

Examples:

>>> file.create(path='tests\demo.txt')
>>> file.read_text(path='tests\demo.txt')
''

delete(path)

Delete a file.

Parameters:

Name Type Description Default
path str || WindowsPath

The path to the file.

required

Returns:

Type Description
None

None

Examples:

>>> file.delete(path='tests\demo.txt')
>>> os.path.exists("tests\demo.txt")
False

get_json_content(path)

Returns the content of the json file.

Parameters:

Name Type Description Default
path str || Windows Path

Path of the json file.

required

Returns:

Name Type Description
content dict

Content of the json file.

Examples:

>>> get_json_content(path='C:\Users\abc\Desktop\file.json')

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:

>>> get_json_section(path='C:\Users\abc\Desktop\file.json', section='name')

is_file(path)

Returns True if the given path is a file.

Parameters:

Name Type Description Default
path str || Windows Path

Path of the file.

required

Returns:

Type Description
bool

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

Examples:

>>> is_file(path='C:\Users\abc\Desktop\file.json')

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:

>>> file.move(source='tests\demo.txt',destination='tests\demo2.txt')
>>> file.read_text(path='tests\demo2.txt')
'This is a demo text file.'

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:

>>> file.read_text(path='tests\demo.txt')
'This is a demo text file.'
>>> file.read_text(path=['tests\demo.txt', 'tests\demo2.txt'])
['This is a demo text file.', 'This is a demo2 text file.']

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:

>>> file.rename(path='tests\demo.txt',new_name= 'demo2.txt')
>>> file.read_text(path='tests\demo2.txt')
'This is a demo text file.'

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:

>>> set_json_section(path='C:\Users\abc\Desktop\file.json', section='name', value='test name')

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:

>>> file.write_text(path='tests\demo.txt',contents= 'This is a demo text file.')
>>> file.read_text(path='tests\demo.txt')
'This is a demo text file.'