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 :: rotate xticks matplotlib 
Python :: pip install contractions 
Python :: pandas dataframe hist title 
Python :: python get keypressed value 
Python :: python detect keypress 
Python :: python input. yes or no 
Python :: pandas display rows config 
Python :: Square of numbers in non-decreasing order 
Python :: celery flower notimplementederror 
Python :: make a message appear after specified Time python 
Python :: keras ensure equal class representation during traingin 
Python :: remove every file that ends with extension in python 
Python :: python wget anaconda 
Python :: pandas show complete string 
Python :: Import "django.core.urlresolvers" could not be resolved 
Python :: how to take password using pyautogui 
Python :: camera lags when using with opencv 
Python :: neural network without training return same output with random weight 
Python :: extract topic to csv file 
Python :: how to make a clicker game in python 
Python :: how to change the favicon in flask 
Python :: how to make a module that generates a random letter in python 
Python :: rotation points space python 
Python :: Python program to find Cumulative sum of a list 
Python :: DataFrame.plot.line() method: | dataframe line plot 
Python :: python diamond pattern 
Python :: python min length list of strings 
Python :: pandas create dataframe of ones 
Python :: python milliseconds to date 
Python :: save video cv2 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =