Python - files

王林
Release: 2024-08-12 18:39:58
Original
669 people have browsed it

Python - files

File operations:

  • File reading
  • File writing
  • appending the content

File Reading:
with open('Logs.txt', 'r') as file:

open is python built in function used to open file. First arguments refers the file name and second argument is mode of reading.
with statement is for automatic closing of file. This will prevent memory leaks, providing a better resource manageement
as file as keyword assigns the opened file object to variable file

with open('logs.txt', 'r')as file:
    # print(file, type(file))
    content = file.readlines()
    print(content, type(content))   # this content is a list. Elements are each line in file 
    for line in content:
        print(line, end='') # end='' is defined to avoid \n as list iteration ends already with \n
        #print(line.strip())
Copy after login

Output:
['This is the file used to store logsn', 'Created on 12.08.2024n', 'Author Suresh Sundararajun']
This is the file used to store logs
Created on 12.08.2024
Author Suresh Sundararaju

  • file.readlines () will give the file content as list
  • file.readline() will give the first line as string

  • Iterating the list, each line can be retrieved as string

  • Iterating the later, each str can be retrieved as character

Here when iterating the list via for loop, the return ends with newline. when printing with print statment, another new line comes. To avoid that strip() or end='' is used

File writing:
with open('notes.txt','w') as file:

This is similar to file reading. the only syntax difference is mode is given as 'w'. Here notes.txt file will be created.

Further to write the content, we can use file.write('Content')
Withe write mode, everytime file will be created and content will be overwriten within that block

# Write in file
with open('notes.txt', 'w') as file:
    i=file.write('1. fILE CREATED\n')
    i=file.write('2. fILE updated\n')
Copy after login

Appending in file:
with open('notes.txt', 'a') as file:

For appending, mode='a' is to be used with file.write(str) or file.writelines(list). Here in the existing file, the content will be updated at the end.

#Append file
with open('notes.txt', 'a') as file:
    file.write('Content appended\n')


#Read all the lines and store in list
with open('notes.txt', 'r') as file:
    appendcontent = file.readlines()
    print(appendcontent)
Copy after login

Output:
['1. fILE CREATEDn', '2. fILE updatedn', 'Content appendedn']

Notes:

  1. There is some other modes available 'r+','w+','a+'
  2. exception can be added

The above is the detailed content of Python - files. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!