“Comment supprimer le tout dernier caractère d'un fichier texte dans Python” Réponses codées

Comment supprimer le tout dernier caractère d'un fichier texte dans Python

file.truncate(file.tell - 1)
#explanation - the function truncate will resize the file to the 
#full size - 1 and that means it will remove the last character
#if you need to do that while you are reading/writing somewhere using
#seek() you can use this function ->
def get_size(fileobject):
    fileobject.seek(0,2) # move the cursor to the end of the file
    size = fileobject.tell()
    return size
#and then
fsize = get_size(file)
file.truncate(fsize - 1)
IC

supprimer la dernière ligne de fichier texte python

fd=open("file.txt","r")
d=fd.read()
fd.close()
m=d.split("\n")
s="\n".join(m[:-1])
fd=open("file.txt","w+")
for i in range(len(s)):
    fd.write(s[i])
fd.close()
Encouraging Eland

Réponses similaires à “Comment supprimer le tout dernier caractère d'un fichier texte dans Python”

Questions similaires à “Comment supprimer le tout dernier caractère d'un fichier texte dans Python”

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code