Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

3.3 Εγγραφη δεδομένων σε αρχείο στην Python

Για να γράψετε σε ένα υπάρχον αρχείο, πρέπει να προσθέσετε ένα παράμετρο στη συνάρτηση open():

  • “a” – Append – θα προσθέσει τα δεδομένα στο τέλος του αρχείου
  • “w” – Write – θα αντικαταστήσει οποιοδήποτε υπάρχον περιεχόμενο

Παράδειγμα για να προσθέσετε στο τέλος ενός υπάρχοντος αρχείου:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example.txt", "a")
f.write("Now the file has more content!")
f.close()
f = open("example.txt", "a") f.write("Now the file has more content!") f.close()
f = open("example.txt", "a")
f.write("Now the file has more content!")
f.close()

Παράδειγμα για να αντικαταστήσετε το περιεχόμενο ενός αρχείου:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
f = open("example.txt", "w") f.write("Woops! I have deleted the content!") f.close()
f = open("example.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()

Σημειώστε ότι η μέθοδος write() δεν προσθέτει αυτόματα ένα νέον στοιχείο γραμμής στο τέλος των δεδομένων που γράφει. Πρέπει να προσθέσετε τον χαρακτήρα \n στο τέλος των δεδομένων, εάν θέλετε να προσθέσετε ένα νέο στοιχείο γραμμής.

Ανοίξτε το αρχείο “example2.txt” και προσθέστε περιεχόμενο στο τέλος του αρχείου:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example2.txt", "a")
f.write("This is some new content added to the file.\n")
f.close()
f = open("example2.txt", "a") f.write("This is some new content added to the file.\n") f.close()
f = open("example2.txt", "a")
f.write("This is some new content added to the file.\n")
f.close()

Αυτό θα προσθέσει το κείμενο “This is some new content added to the file.” στο τέλος του αρχείου “example2.txt”. Προσέξτε ότι προσθέσαμε και τον χαρακτήρα \n στο τέλος του κειμένου για να δημιουργήσουμε ένα νέο στοιχείο γραμμής.

[adinserter block=”2″]

Μπορούμε να διαβάσουμε τα περιεχόμενα του αρχείου “example2.txt” και να τα εμφανίσουμε στο τερματικό χρησιμοποιώντας τη μέθοδο read() και τη συνάρτηση print():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example2.txt", "r")
print(f.read())
f.close()
f = open("example2.txt", "r") print(f.read()) f.close()
f = open("example2.txt", "r")
print(f.read())
f.close()

Αυτό θα εμφανίσει όλο το περιεχόμενο του αρχείου “example2.txt”. Αν το περιεχόμενο του αρχείου είναι το εξής:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is some content in example2.txt.
This is some new content added to the file.
This is some content in example2.txt. This is some new content added to the file.
This is some content in example2.txt.
This is some new content added to the file.

Τότε το αποτέλεσμα της print() θα είναι:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is some content in example2.txt.
This is some new content added to the file.
This is some content in example2.txt. This is some new content added to the file.
This is some content in example2.txt.
This is some new content added to the file.

Μπορούμε να ανοίξουμε το αρχείο “example3.txt” και να αντικαταστήσουμε το περιεχόμενο του με το επιθυμητό, χρησιμοποιώντας τη μέθοδο write():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example3.txt", "w")
f.write("This is the new content that will replace the old one.")
f.close()
f = open("example3.txt", "w") f.write("This is the new content that will replace the old one.") f.close()
f = open("example3.txt", "w")
f.write("This is the new content that will replace the old one.")
f.close()

Αυτό θα αντικαταστήσει το περιεχόμενο του αρχείου “example3.txt” με το κείμενο “This is the new content that will replace the old one.”. Προσέξτε ότι αν υπάρχει ήδη περιεχόμενο στο αρχείο, αυτό θα αντικατασταθεί από το νέο περιεχόμενο.

[adinserter block=”3″]

Μπορούμε να διαβάσουμε τα περιεχόμενα του αρχείου “example3.txt” και να τα εμφανίσουμε στο τερματικό χρησιμοποιώντας τη μέθοδο read() και τη συνάρτηση print():

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example3.txt", "r")
print(f.read())
f.close()
f = open("example3.txt", "r") print(f.read()) f.close()
f = open("example3.txt", "r")
print(f.read())
f.close()

Αυτό θα εμφανίσει όλο το περιεχόμενο του αρχείου “example3.txt”. Αν το περιεχόμενο του αρχείου είναι το εξής:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is the new content that will replace the old one.
This is the new content that will replace the old one.
This is the new content that will replace the old one.

Τότε το αποτέλεσμα της print() θα είναι:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This is the new content that will replace the old one.
This is the new content that will replace the old one.
This is the new content that will replace the old one.

Για να δημιουργήσετε ένα νέο αρχείο στο Python, χρησιμοποιήστε τη μέθοδο open(), με έναν από τους ακόλουθους παραμέτρους:

“x” – Δημιουργία – θα δημιουργήσει ένα αρχείο, επιστρέφει ένα σφάλμα εάν το αρχείο υπάρχει ήδη.

“a” – Προσάρτηση – θα δημιουργήσει ένα αρχείο εάν το συγκεκριμένο αρχείο δεν υπάρχει.

“w” – Εγγραφή – θα δημιουργήσει ένα αρχείο εάν το συγκεκριμένο αρχείο δεν υπάρχει.

Εδώ είναι ένα παράδειγμα που δημιουργεί ένα νέο αρχείο με όνομα “example4.txt” και γράφει μια γραμμή στο αρχείο:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
f = open("example4.txt", "w")
f.write("This is some text that will be written to the file!")
f.close()
f = open("example4.txt", "w") f.write("This is some text that will be written to the file!") f.close()
f = open("example4.txt", "w")
f.write("This is some text that will be written to the file!")
f.close()

Αυτό θα δημιουργήσει ένα νέο αρχείο με όνομα “example4.txt” και θα γράψει την πρόταση “This is some text that will be written to the file!” σε αυτό. Προσέξτε ότι αν υπάρχει ήδη ένα αρχείο με το όνομα “example4.txt”, αυτό θα αντικατασταθεί από το νέο αρχείο που δημιουργήθηκε.

Δωρεα μεσω Paypal

Για την κάλυψη αναγκών φιλοξενίας και δημιουργίας περιεχομένου.

κατηγοριες μαθηματων

Ιστορικο ενοτητων

top
error: Content is protected !!
Μετάβαση σε γραμμή εργαλείων