Tags
python
Asked 2 years ago
12 Aug 2021
Views 216
Evalyn

Evalyn posted

How do I save a file in Python path ?

How do I save a file in Python path ?
jai

jai
answered Aug 17 '21 00:00

Following is Python code to save file with Path

from pathlib import Path
paths =Path('save.txt')
paths.write_text('test is good')
print(paths.read_text())


Path is a subclass of PurePath, this class represents concrete paths of the system’s path
1. import Path from pathlib

2. make Path Object

paths =Path('put file path here')


3. use write_text method to write the content into the file.

paths.write_text('test is good')


to confirm write text works or not, i use read_text method to read the file to confirm
in this case if the file is not there it will generate the file without generating error and if the file is already there than it overwrite the text to the file

Post Answer