Skip to content

File

File module for dost. This module is used to interact with files.

Examples:

>>> from dost import file
>>> file.read_text(path='tests\demo.txt')
'This is a demo text file.'

It contains the following functions:

  • 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.

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

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

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

Examples:

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

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

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:

Type Description
Union[str, List[str]]

(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

Examples:

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

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

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.'