python


reading last line of txt file in python and change it into variable to make calculation


td = 'date of transaction.txt'
tf = 'current balance.txt'
tr = 'transaction record.txt'
for line in open(tf):pass
for line2 in open(td):pass
for line3 in open(tr):pass
print line2,line,line3
"""
so call recall back last record
"""
rd=raw_input('\ndate of transaction: ')
print "Choose a type of transaction to proceed... \n\tA.Withdrawal \n\tB.Deposit \n\tC.Cancel & exit"
slc=raw_input('type of transaction: ')
i=1
while (i>0):
if slc=="A" or slc=="B" or slc=="C":
i=0
else:
i=i+1
slc=raw_input('invalid selection, please choose again...(A/B/C): ')
if slc=="A":
rr=input('\namount of transaction: ')
Line_len = 10 # or however long a line is, since in my example they all looked the same
SEEK_END = 2
file = open(tf, "r")
file.seek(-Line_len, SEEK_END)
a = int(str(file.read(Line_len)).split(" ")[0].strip())
rf=a-rr
f1=open(tf, 'a+')
f1.write('\n'+rf)
f1.close()
d1=open(td, 'a+')
d1.write('\n'+rd)
d1.close
r1=open(tr, 'a+')
r1.write('\n-'+rr)
r1.close
else:
print 'later'
above is my code, the function is to get data(last line) from txt file and read it, get new data and write it to the txt file again by creating new line.
my txt file(current balance.txt) should look like this:
2894.00
2694.00
but when i try to use the last line which is 2694.00 to do calculation(rf=a-rr), it failed returning this error:
Traceback (most recent call last):
File "C:\Python27\acc.py", line 27, in <module>
file.seek(-Line_len, SEEK_END)
IOError: [Errno 22] Invalid argument
else if i use this code:
for line in open(tf):
pass
a = line
rf=a-rr
it return this error:
Traceback (most recent call last):
File "C:\Python27\acc.py", line 27, in <module>
rf=a-rr
TypeError: unsupported operand type(s) for -: 'str' and 'int'
I seriously have no idea why...please help me...
To obtain last line of the file, you can simple do
with open('my_file.txt') as file:
last_line = file.readlines()[-1]
#last_line is a string value pointing to last line, to convert it into float, you can do
number = float(last_line.strip('\n').strip(' '))
The function input is giving you a string. Try doing:
rf=a-float(rr)

Related Links

How to create random orthonormal matrix in python numpy
Fill an image with pre-defined pattern with Wand
How to use a post_save receiver with django-polymorphic?
giving a name to a pandas dataframe?
mistake comparing datetime, python
numpy testing assert array NOT equal
How to join threads of an asyncio executor runned inside coroutine?
Google Admin SDK insert custom schema using python
Importing images for manifold Isomap
Having nosetests watch files from outside container and rerun tests inside container
Trying to ftp from my system to remote server
Attribute in a Class immediately runs at the wrong time?
Unable to handle “HTTP Badstatus line” exception
How to merge/join two resultset/table in django?
Maximise the Slope using CVXPY
Python-Instagram user_recent_media returning next of none

Categories

HOME
arrays
visual-studio-2015
plsql
crate
wildcard
nuxeo
backup
steam
decorator
bibtex
search-engine
safari
xtext
bro
data-science-experience
openwrt
android-viewpager
ipfs
unity2d
x11
rfid
spinnaker
intellij-plugin
data-synchronization
phoenix
jquery-terminal
ada
google-openid
jcl
atlassian-plugin-sdk
movies
plupload
k2
searchbar
java-ee-7
ab-initio
frp
aurelia-binding
picturebox
oracle-xml-db
preg-grep
division
rhomobile
jboss-esb
autocompletetextview
vmd
axis-labels
infusionsoft
socketscan
onbackpressed
paho
rkt
bids
3scale
startapp
automator
tuleap
nstouchbar
ti-basic
crystal-reports-8.5
crash-reports
paas
collapsingtoolbarlayout
pdfkit
atlassian-crowd
inject
cjson
backstop.js
picasa
vst
rstudio-server
wcf-ria-services
freetype2
com-interop
spinlock
flexjson
axes
altbeacon
sem
windowlistener
built-in
pyrocms
base32
ytplayerview
pgm
callstack
ember-cli-addons
gmaps4rails
hidden-field
cg
node-inspector
ember-components
sthttprequest
author
yahoo-boss-api
boost-test
mfmailcomposeviewcontroll
colt
cppdepend
mbox
bsod
windows-mobile-6
surveyor-gem
fortran77
fluidsynth
maven-ear-plugin
couchdb-lucene
javascriptserializer
xmlslurper
userid
hobo
nssavepanel
windows-live-id
port-scanning
noir
zend-form-element
gwt-2.2-celltable
longjmp
virtualquery

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App