how to connect with files in python programming?

  • برمجة
  • برمجة بايثون

how to connect with files in python programming?

الأجوبة

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")

 

هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...