May-02-2018, 04:06 AM
Hello, I am new to this forum. I joined so that I could ask for help with this program. First, I will detail what it does.
I get these search files that look a bit like this:
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2001.epub ::INFO:: 344.3KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2001.zip ::INFO:: 373.7KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.epub ::INFO:: 493.9KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.mobi ::INFO:: 546.6KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.zip ::INFO:: 297.2KB
I only have a use for certain types of files. Now, I can get my script to filter one or several at a time by using a for in loop. I thought that if I added a second loop, I could get them sorted into different piles. So, here is the code:
*****************************
I also have a second question that I have been thinking about: I have thought about building a library of authors that
sometimes I want to search for. So, I wondered if using a similar formula to what I have already but as a function where
the loop iterates through the dictionary looking for and returning lines which contain the name of the given author. Is
that a valid way of going about it, or should I be seeking a different path?
Any help is appreciated,
Steven
I get these search files that look a bit like this:
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2001.epub ::INFO:: 344.3KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2001.zip ::INFO:: 373.7KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.epub ::INFO:: 493.9KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.mobi ::INFO:: 546.6KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.zip ::INFO:: 297.2KB
I only have a use for certain types of files. Now, I can get my script to filter one or several at a time by using a for in loop. I thought that if I added a second loop, I could get them sorted into different piles. So, here is the code:
*****************************
import re # input the name of the file input_file_name = input("Enter file name: ") input_file_name = input_file_name + ".txt" input_open = open(input_file_name) # create new file name file_type_removed = input_file_name[:-4] # removes .txt from input file name add_modified_to_new_file = "_modified.txt" # creates the end part of the new file name new_text = file_type_removed + add_modified_to_new_file # puts the file extension and modified to the new file new_file_create = open(new_text, "w+") # should create a new file with this name # finds all the lines with mobi in them and writes those lines to the new file for line in input_open.readlines(): if re.search('mobi|azw', line): new_file_create.write(line) # This second loop will not run or return the results as I expected. Only the first loop # returns a text file with the expected results. I would like to understand why and how # I might fix it. for line in input_open.readlines(): if re.search('pdf', line): new_file_create.write(line) # Close the files new_file_create.close() input_open.close()***********************************
I also have a second question that I have been thinking about: I have thought about building a library of authors that
sometimes I want to search for. So, I wondered if using a similar formula to what I have already but as a function where
the loop iterates through the dictionary looking for and returning lines which contain the name of the given author. Is
that a valid way of going about it, or should I be seeking a different path?
Any help is appreciated,
Steven