Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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)
Comment

remove last line of text file python

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

PREVIOUS NEXT
Code Example
Python :: python soap 
Python :: django dockerfile multistage 
Python :: python from float to decimal 
Python :: filter query objects by date range in Django? 
Python :: 2d gaussian function python 
Python :: pandas profile report python 
Python :: empty dictionary python 
Python :: python collections Counter sort by key 
Python :: % operatior in python print 
Python :: move items from one list to another python 
Python :: Pandas categorical dtypes 
Python :: numpy vector multiplication 
Python :: socket io python 
Python :: check if all characters in a string are the same python 
Python :: convert base64 to numpy array 
Python :: how to convert the date column from string to a particular format in python 
Python :: count number of each item in list python 
Python :: python asctime 
Python :: outlier removal 
Python :: pygame tick time 
Python :: python rdp server 
Python :: python how to calculate how much time code takes 
Python :: reshape wide to long in pandas 
Python :: declare empty array of complex type python 
Python :: Find the title of a page in python 
Python :: find factorial in python 
Python :: python datetime format string 
Python :: filter one dataframe by another 
Python :: list of python keywords 
Python :: how to delete all instances of a model in django 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =