Python Faira Kubata

Mune ino posvo tichakurukura nzira dzePython dzekubata maFaira. Iyi inotevera kodhi mienzaniso inoratidza maitiro ekugadzira, kuverenga, kunyora uye kudzima mafaera muPython.

Maitiro Ekugadzira Mafaira muPython

Kugadzira faira muPython, tinoshandisa open() nzira, iyo inotora maparameter maviri: zita refaira uye chero imwe yemamodhi: 'x', 'a', 'w'.

'x' inoshandiswa kugadzira faira nyowani. Iko kukanganisa kunokandwa kana iyo faira iripo. 'a' uye 'w' anoshandiswa kunyorera kune iyo faira uye kunyora kune iyo faira, zvichiteerana, zvisinei kana iyo faira isipo, ipapo iyo faira inogadzirwa

Muenzaniso:

file = open('somefile.txt', 'x')

Faira nyowani somefile.txt zvakasikwa.

Maitiro Ekuverenga Mafaira muPython

Kuti uverenge faira muPython, tinoshandisa open() basa, kupfuura muzita refaira uye 'r' yekuverenga modhi.



Semuenzaniso: verenga faira inonzi somefile.txt

Zviri mukati me somefile.txt:

Hello!! Welcome to Python Goodbye. file = open('somefile.txt', 'r') print(file.read()) file.close()

Kubuda:

Hello!! Welcome to Python Goodbye.

Maitiro Ekuverenga Zvikamu zveFaira muPython

Tinogona kuverenga zvikamu zvefaira nekupfuura muhuwandu hwevatambi kuenda read() nzira. Semuyenzaniso:

file = open('somefile.txt', 'r') print(file.read(5)) file.close()

Kubuda:

Hello

Maitiro Ekuverenga iyo Faira Mutsetse Neraini

Tinogona kushandisa readline() nzira yekuverenga yega yega tambo yefaira.

Verenga Mutsara Mumwe Chete

file = open('somefile.txt', 'r') print(file.readline()) file.close

Kubuda:

Hello!!

Verenga Mitsara miviri

file = open('somefile.txt', 'r') print(file.readline()) print(file.readline()) file.close

Kubuda:

Hello!! Welcome to Python

Verenga Zvese Mitsara

Tinogona kushandisa for chiuno kuverenga mitsara yese yefaira:

file = open('somefile.txt', 'r') for x in file:
print(x)

Kubuda:

Hello!! Welcome to Python Goodbye

Maitiro ekunyorera kune iyo faira muPython

Kunyorera kune faira, tinoshandisa zvakare open() nzira ine zita refaira seyokutanga paramende uye kana 'a' kana 'w' separamende yechipiri.

'a' ichawedzera data kune iripo faira yakatsanangurwa. 'w' ichawedzera data pane yakatarwa faira.

Muzviitiko zvese izvi, iyo faira inogadzirwa kana isipo.

Nyorera kune Nyowani File

file = open('writefile.txt', 'w') file.write('Write some content!') file.close()

Kubuda:

writefile.txt yakagadzirwa nezviri mukati:

Write some content! Ongorora:Kana iyo faira isipo, ichagadzirwa. Kana faira riripo, zvirimo mufaira zvinonyorwa!

Nyorera zvirimo kune iyo Iripo Faira

Kuisa zvirimo kune raive faira, tinofanirwa kupfuura mu 'a' paramende kune open() nzira yeappend modhi.

file = open('writefile.txt', 'a') file.write(' Write more content!') file.close()

Zviri mukati me writefile.txt faira:

Write some content! Write more content!

Maitiro Ekudzima mafaera muPython

Kudzima mafaera, isu tinofanirwa kuendesa iyo os module uye shandisa remove() nzira:

import os if os.path.exists('writefile.txt'):
os.remove('writefile.txt')

Maitiro ari pamusoro anotanga kutarisa kuti faira riripo usati waedza kuidzima. Iko kukanganisa kunokandwa kana iyo faira isipo.