I'm trying to write a program that will take a text file and make another version, limitited to 79 characters per line. The program runs fine, it successfully opens the input file, and it creates the clipped.txt file. It prints the contents of the input file onscreen, but there's nothing in clipped.txt.
I am wondering if there is a problem with the way I am handling the strings. Look at how the word string is handled under the main loop: I think there may be some problem with using Python strings that I don't understand.
#Clipper v 0.1
import os, sys
from os.path import *
while True:
path = raw_input("Enter path of file, or type \"q\" to quit: ")
if path is "q":
sys.exit()
elif isfile(path) is not True:
print "This file does not exist."
else: break
file = open(path,"r")
print path + "\n opened successfully."
raw_input()
tempfile = open("E:\\clipped.txt", "w")
print "Temp file opened successfully."
raw_input()
#Remembers how many characters have already been written to a line.
line_limit = 79
line_pos = 0
word = ''
char = ''
#The main loop, which will run until break
while True:
char = file.read(1)
print char #Just so I can see that it's working.
#I want to break the input into separate words.
#If it's not a space, add it to the word string.
if char is not ' ':
word += char
else:
#When python finds a space, it will stick the word onto the current
#line if there's enough room.
if len(word) + line_pos <= line_limit:
tempfile.write(word)
tempfile.write(" ")
line_pos = line_pos + len(word)
word = ""
else:
#Else it will start a new line and write it there.
tempfile.write('\n')
tempfile.write(word)
tempfile.write(" ")
line_pos = len(word)
word = ""
if not char:
#Quit when EOF is reached.
break
This post has been edited by not1975: 04 April 2008 - 02:17 PM

New Topic/Question
Reply




MultiQuote






|