Working with file objects: open function, reading and writing to files in Python

Files belong to the basic data types of the Python language. This is the connecting interface between code and named areas of computer memory. Files perform the function of "canning" objects. They allow you to save any information with its subsequent unloading and processing. We’ll look at how to write to a file in Python and read data back with practical examples.

write to file in python




How to open a file?

Work with this data type starts with the built-in open function. It creates a file object that provides communication with an external document on the computer. After you call the function, you can perform read or write operations to files in Python.

For beginners, working with files will seem difficult. They differ from the usual lines, numbers, dictionaries. To interact with them, literals of sequences or mappings are not used, only methods.

, . , output.flush(), . , .

open(), , :





  • r – , ;
  • w – Python;
  • a – ;
  • b – ;
  • «+» .

– , :

  • >>>This_file = open(«C:\odd», «w») # open .

python write string to file




?

, . . Python .

:

  • input.read() – ;
  • input.readline() – ;
  • input.readlines() – ;
  • .write() – ;
  • .writelines() – ;
  • .close() – .

writing a dictionary to a python file




, , . , . int() list(). .





close . flush . . Python, , .

python json write to file




. – IDLE. :

  • >>>_ = open(«_.txt», «w»)
  • >>>_.write(« \n») #
  • 12
  • >>>_.write(« \n»)
  • 20
  • >>>_.close() #;
  • >>>_ = open(«_.txt»)
  • >>>_.readline() #, ,
  • « \n»

, Python 3.0 . . \n. write .

\n , read:

  • >>>print(open(«_.txt»).read())
  • ;
  • ;

:

  • >>>for x in open(«_.txt»):
  • print(x, end = « »)
  • # .

Python

. Python , . , .

  • >>>_2 = open(«_.txt», «w»)
  • >>> = [1,8, «r»]
  • >>>C, B, P = 34, 90, 56
  • >>> = « »
  • >>>_2.write(str() + «\n»)
  • 12
  • >>>_2.write(«%s, %s, %s\n» % (C, B, P))
  • 11
  • >>>_2.write( + «\n»)
  • 15
  • >>>_2.close()
  • >>>print(open(«_.txt»).read())
  • [1, 8, «r»]
  • 34, 90, 56

«_.txt» . , . . print read.

python write to file line by line




Python . str . , .

readline:

  • >>>L = open(«_.txt»)
  • >>>F = L.readline()
  • >>>F
  • «34, 90, 56\n»
  • >>>Numbers = F.split(«,») #
  • >>>Numbers
  • [«34», «90», «56\n»]
  • >>>Numbers = [int(x) for x in Numbers] #
  • >>>Numbers
  • [34, 90, 56]

pickle. . , .

, . (), pickle:

  • >>> = {«»: 8, «»: 3, «»: 0}
  • >>>
  • {«»: 8, «»: 3, «»: 0}
  • >>> = open(«_.pkl», «wb»)
  • >>>import pickle
  • >>>pickle.dump(, )
  • >>>.close()
  • >>> = open(«_.pkl», «rb»)
  • >>> = pickle.load()
  • >>>
  • {«»: 8, «»: 3, «»: 0}

. . open wb – write binary.

pickle, «» Python – JSON. dump. , JSON-.

  • >>>import json
  • >>> = {«»: 8, «»: 3, «»: 0}
  • >>>with open(«_.json», «w») as _:
  • >>>json.dump(, _)

There are more complex ways to work with files. For example, a scan operation, organization of recording cycles. To see the full list of methods, use the help or dir functions in an interactive session. Also in the arsenal of the language there are objects similar to files - sockets, threads of the shell and I / O.




All Articles