Python Scripting How To Open Read & Write In File

Python Scripting For Storage Admin - How To Read & Write In File. 

Python Scripting How To Open Read & Write In File
Python Scripting How To Open Read & Write In File

In this videos we will see how we can open file using python script then we will write some content in the file and then save it using python script. This is also called as file handling in python script.

Below is the code which is explained in above video.

Below line will open a file for writing purpose. You can use "r" for read and "a" for append.

s=open("file.txt","w")

This line will write some content in the file. \n is the newline character which writes data in a new line.

s.write("this is a file2\n")

Below line will save the data and close the file. If you miss below line then data will not get saved.

s.close()

This line will read the entire content of file and store it in a variable read_file.

read_file=open("file.txt","r").read()

Below line will print the content which is stored in variable read_file.

print(read_file)      

For more details and live demo please refer to the below video. Subscribe to youtube channel as well and stay tuned for more videos in this video series.

                

No comments:

Post a Comment

Zoning In Cisco MDS SAN Switch In Command Line

Zoning In Cisco MDS SAN Switch In Command Line Zoning is a process of grouping initiator and target ports WWPN which is performed in SAN ...