ΚΑΛΩΣΗΡΘΑΤΕ ΣΤΟ BLOG ΜΑΣ!
It's our world...0 or 1 Enjoy your stay!


Στείλτε e-mail,στο unitedprogrammersbspot@gmail.com



(Πληκτρολογώντας εδώ,στέλνετε e-mail στο unitedprogrammersbspot@gmail.com)

©No Copyright.Copy or distribute for free




Πέμπτη 11 Ιουλίου 2013

ΕΓΓΡΑΦΗ ΣΕ ΑΡΧΕΙΟ-PYTHON

Ας δούμε πως μπορούμε να δημιουργήσουμε και να γράψουμε σε ένα αρχείο,προγραμματίζοντας στην Python.

Εδώ είναι ένα απλό πρόγραμμα ,ώστε να γίνει κατανοητή η χρησιμότητα,αλλά και να μάθουμε πως.

# -*- coding= utf8 -*-        ---->το γράφουμε, για  να 'εμφανίζονται' τα ελληνικά.
prwino=str(input("ΠΡΩΙΝΟ:")) ---->δέχεται είσοδο απο πληκτρολόγιο,το πρωινό 

και αποθηκεύεται στην μεταβλητή 'prwino'
arxeio=open("fagito.txt","a+") ---->δημιουργεί το αρχείο 'fagito.txt'.Το a+,θα εξηγήσουμε σε λίγο τι σημαίνει.
arxeio.write(prwino) ---->Εδώ, γράφουμε μέσα στο αρχείο την λέξη που δώσαμε ως 'πρωινο',αφού αποθηκεύτηκε στην μεταβλητή prwino.
arxeio.write("\n") ---->Εδώ, απλά αφήνουμε μια κενή σειρά στο αρχείο.

meshmeriano=str(input("ΜΕΣΗΜΕΡΙΑΝΟ:"))---->δέχεται είσοδο απο πληκτρολόγιο,το μεσημεριανό και αποθηκεύεται στην μεταβλητή 'meshmeriano'
arxeio.write(meshmeriano) ---->
Εδώ, γράφουμε μέσα στο αρχείο την λέξη που δώσαμε ως 'μεσημεριανό',αφού αποθηκεύτηκε στην μεταβλητή meshmeriano.
arxeio.write("\n") 
---->Εδώ, απλά αφήνουμε μια κενή σειρά στο αρχείο.
arxeio.write("===========")---->Εδώ ,γράφουμε μέσα στο άρχειο "===========".

Το a+ είναι μια επιλογή η οποία :
Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Υπάρχουν και άλλες επιλογές,όπως είναι :

rOpens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.               
rbOpens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
r+Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
rb+Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
wOpens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
wbOpens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
w+Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
wb+Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
aOpens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
abOpens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.  
a+Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.   

Πηγή :http://www.tutorialspoint.com/python/python_files_io.htm
(Πολύ χρήσιμος σύνδεσμος)


Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

Μερικά χρήσιμα βίντεο...