File handling in python

 

Data usually are stored in files and using python to work with the files is most important aspect

 

For dealing with a file we have to create an object file as below

file= open("filename","mode")

mode can be -

 "r" read,"w" - write , "a"- append,"x' -create


Please not 

  • if the file is open we have to close the same using close method 
  • read using read method as shown below
 
# Open a file: file
file = open('moby_dick.txt','r')

# Print it

print(file.read())


# Check whether file is closed
print(file.closed)

# Close file
file.close()

# Check whether file is closed
print(file.closed)


Output 





Comments

Popular posts from this blog

How to start Python for Data science | High level overview