“pliki Python” Kod odpowiedzi

Otwórz plik tekstowy w Python

f=open("Diabetes.txt",'r')
f.read()
Grieving Goshawk

Pliki Pythona

f = open('filename.txt', 'r') #open for reading (default)
f = open('filename.txt', 'w') #open for writing, truncating the file first
f = open('filename.txt', 'x') #open for exclusive creation, failing if the file already exists
f = open('filename.txt', 'a') #open for writing, appending to the end of the file if it exists
Jsandtrooper

Plik Python otwarty

#there are many modes you can open files in. r means read.
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()

#you can write a string to it, too!
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')

#don't forget to close it afterwards!
file.close()
Dr. Hippo

Czytanie pliku Pythona

fin = open("NAME.txt", 'r')
body = fin.read().split("\n")
line = fin.readline().strip()
Bewildered Baboon

pliki Python

with open("filename.txt", "w") as file:
  file.write("new line in new/now cleared document")
with open("filename.txt", "a") as file:
  file.write("This line is added to the now 2 line document")
with open("filename.txt", "r") as file:
  lines = file.readlines() # ["new line in new/now cleared document\n", "This...
fmoeran

Plik Python

>>> f = open('workfile', 'w')
Shy-ny Star-ling

Odpowiedzi podobne do “pliki Python”

Pytania podobne do “pliki Python”

Więcej pokrewnych odpowiedzi na “pliki Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu