[Tutor] Working Script...but!
Ross Glover
ross at ross.mayfirst.org
Thu May 29 23:17:23 CEST 2008
Hi all,
I've worked out this script to do what I want generally. However, I'm
pretty sure that even though it works, I haven't utilized the advantages
of python. This is more or less my first program. What I would like to
know is what parts of this script you think could or should be functions
in their own right. Also, I'm trying to figure out if I should be
creating this as a class. I know that I still need to create some
exception handling and stuff, but I want to get these functions as well
articulated as possible. I hope it's okay that I submitted this much
script.
Thanks for any suggestions,
Ross
#!/usr/bin/env python
# Filename: read_tags.py
import os
import re
file_name = raw_input("Name of file to read: ")
read_path = "/home/ross/Desktop/%s.txt" % file_name
read_file = file(read_path, "r")
search_file = read_file.read()
def write_file(string_to_write):
'''This function writes a string to a specified file using append
and taking data from the string given it.'''
if string_to_write != []:
write_to_filename = raw_input("Name of file to write: ")
path_input = raw_input("Path to file: ")
if path_input == "":
default_path = "/home/ross/Desktop/"
write_to_path = default_path + "%s.txt" % write_to_filename
write_to_file = file(write_to_path, "a")
for items in string_to_write:
write_to_file.write(items + '\n')
continue
print "File %s written!" % path_input
else:
write_to_path = path_input+ "%s" % write_to_filename
write_to_file = file(write_to_path, "a")
for items in string_to_write:
write_to_file.write(items + '\n')
print "File %s written!" % write_to_path
else:
print "There is nothing to write."
def search_text():
if search_file != False:
tag_to_search = raw_input("Name the theme: ")
rawstr = "(?<=</%s>).*(?=<%s>)|(?<=</%s>).*[\\n]+[\S\s[<%s>]+?]*(?=<%s>)" % (tag_to_search, tag_to_search, tag_to_search, tag_to_search, tag_to_search)
search_result = re.findall(rawstr, search_file)
write_file(search_result)
#print search_result
return search_result
else:
print "File is empty!"
--
If you know what you're doing, you must not be 'doing' a dissertation.
More information about the Tutor
mailing list