Answers for "python delete last character in file"

64

python remove last character from string

str =  "string"
str = str[:-1]  # Returns "strin"
Posted by: Guest on July-25-2020
1

how to remove the very last character of a text file in 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)
Posted by: Guest on January-09-2021
2

python remove last character from string

your_string = "hello"
your_string = your_string[:-1] # this removes the last character from your string
Posted by: Guest on October-10-2020
0

remove last line of text file 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()
Posted by: Guest on August-18-2020

Code answers related to "python delete last character in file"

Python Answers by Framework

Browse Popular Code Answers by Language