Wednesday, 15 April 2015

Get the last character of a String from a file in Python? -



Get the last character of a String from a file in Python? -

i'm trying write function takes character , places words in ifile ends character c new file. thought based on string indexing, text file result blank. why line[0] work line[-1] won't? don't understand why same concept doesn't apply.

def getlistend(c,ifile,ofile): line in ifile: if c in line[-1]: ofile.write(str(line))

make sure phone call ofile.close() in same scope wrote ofile = open('some_file.txt', 'w'), after phone call getlistend of course.

so you'll have code looking this:

def getlistend(c,ifile,ofile): line in ifile: if c in line[-1]: ofile.write(str(line)) infile = open('inputfile.txt', 'r') outfile = open('outputfile.txt', 'w') getlistend('$', infile, outfile) outfile.close() infile.close()

if don't phone call close(), buffers might not flushed correctly , file won't written to.

string python-3.x file-io

No comments:

Post a Comment