Reading multiple lines from an external text file in Python -


this program works fine when use code

for character in infile.readline():   

problem readline reads 1 line of text. when add "s" readline command

for character in infile.readlines():   

i end getting 0's output.

os.chdir(r'm:\project\count')  def main():     infile = open("module3.txt","r")     uppercasecount = 0     lowercasecount = 0     digitcount = 0     spacecount = 0     character in infile.readlines():         if character.isupper() == true:             uppercasecount += 1         if character.islower() == true:             lowercasecount += 1         if character.isdigit() == true:             digitcount += 1         if character.isspace() == true:             spacecount += 1      print ("total count %d upper case, %d lower case, %d digit(s) , %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))  main() 

also if give me advice, can take directory , make default location can take , use on else's machine.

if want check type of caracters contained in file, wouldn't use readlines regular read.

step_bytes = 1024  def main():     infile = open("module3.txt","r")     uppercasecount = 0     lowercasecount = 0     digitcount = 0     spacecount = 0     data = infile.read(step_bytes)     while data:         character in data:             if character.isupper() == true:                 uppercasecount += 1             if character.islower() == true:                 lowercasecount += 1             if character.isdigit() == true:                 digitcount += 1             if character.isspace() == true:                 spacecount += 1         data = infile.read(step_bytes)      print ("total count %d upper case, %d lower case, %d digit(s) , %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))  main() 

if need use readlines, keep in mind that method read lines of file , put them in memory (not large files) in list of lines.

for instance, assuming module3.txt file contains:

this test , line 

using readlines() return:

['this test\n', 'and line'] 

with in mind, can walk file contents using double for loop:

def main():     infile = open("module3.txt","r")     uppercasecount = 0     lowercasecount = 0     digitcount = 0     spacecount = 0     lines = infile.readlines()     line in lines:         character in line:             if character.isupper() == true:                 uppercasecount += 1             if character.islower() == true:                 lowercasecount += 1             if character.isdigit() == true:                 digitcount += 1             if character.isspace() == true:                 spacecount += 1     print ("total count %d upper case, %d lower case, %d digit(s) , %d spaces." %(uppercasecount, lowercasecount, digitcount, spacecount))  main() 

as directory thing, if code , text file (module3.txt) going shipped in same directory, don't need chdir. default, working directory of script directory script is.

let's ship in directory like:

  |-> count      |-> script.py      |-> module3.txt 

you can use relative paths open module3.txt within script.py: line open("module3.txt", "r") go file called module3.txt withing directory script running (meaning, count\ ). don't need call os.chdir. if still want make sure, chdir directory script located (take this):

knowing that, change hardcoded chdir line ( os.chdir(r'm:\project\count') on top of file) to:

print "changing current directory %s" % os.path.dirname(os.path.realpath(__file__)) os.chdir(os.path.dirname(os.path.realpath(__file__))) 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -