Q:

how to connect with files in python programming?

0

how to connect with files in python programming?

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Files in pythons:

------------------

to communicate with files in python there are 3 steps:

1.opening a file

2.perform operations (Read or write on file)

3.close the file

 

what is the syntax for opening a file in python?

file object=open(file_name, access_mode)

 

Example:

f=open("test.txt")

 

syntax of closing connection with file in python:

fileObject.close()

example:

f=open("test.txt","wb")
f.close()

 

writing in a file:

syntax:

fileObject.write(string)

example:

f=open("test.txt","w")
f.write('writing to the file line 1\n')
f.write('writing to the file line 1\n')
f.close()

 

Reading a file:

syntax:

fileObject.read([size])

example:

f=open("test.txt","r")
print(f.readline())
f.close()

 

Renaming a file:

example:

import os
os.rename("test.txt","new.txt")

 

Deleting a file:

example:

import os
os.remove("test.txt")

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now