Posts: 53
Threads: 14
Joined: Mar 2017
Hey everyone just a quick question. I need to omit lines from a file where the number of lines is dictated by the user input then save it as a new file
I have the jist of editing files and saving them which is fine I would just really appreciate some clarification on how to remove lines since im not sure if there is a delete funtion...
Here is what I have so far maybe I am missing something simple.
import sys
print(" *** Truncating File Copy ***")
infile_name = input("Please enter the name of the file to copy: ")
outfile_name = input("Please enter the name of the new copy: ")
infile = open(infile_name, 'r', errors = 'ignore')
outfile = open(outfile_name, 'w')
def lines(): [i for i in infile.readlines()]
lines()
outfile = open(outfile_name, 'w')
def omit():
omitline = (input("Omit how many lines from the start:"))
for num in omitline:
outfile.write('\t')
infile = open(infile_name, 'r', errors = 'ignore')
outfile = open(outfile_name, 'w')
omit()
outfile.write(infile.read())
infile.close()
outfile.close()
Posts: 3,458
Threads: 101
Joined: Sep 2016
You're not really deleting anything. "skipping" a few lines just means you'll be reading from the infile, but not writing to the outfile for a few lines. So writing a tab character doesn't make sense (you should be reading, not writing).
Aside from that...
- you open a bunch of file handles, and only close some of them (you don't need to open either infile or outfile more than once... and with blocks are your friend)
- I don't know why the function lines() exists... or why you call it.
- in omit(), you have omitline = "some string". You should call int() on it, as right now you're looping over how many characters they type, not whatever number they give (so it'll probably always be 1, unless they type at least 10).
Posts: 53
Threads: 14
Joined: Mar 2017
May-11-2017, 09:47 PM
(This post was last modified: May-11-2017, 10:26 PM by Liquid_Ocelot.)
Seems I am still rather stuck on this. I have worked out how to add lines properly but I still am stumped by replacing the lines.
The instructions are stating:
Ask the user two questions before copying:
*** Truncating File Copy ***
Omit how many lines from the start: 2
Omit how many lines from the end: 3
The function then copies the file omitting the specified lines.
Note:
Warning the user if the input file is too short (i.e. so that after omitting the specified lines, the output file would be empty). In this can, display a message and exit without writing the output file.
i.e. the number of lines in the file needs to be greater than (omitStartCount + omitEndCount) lines long (i.e. >5 lines long for the above example)
make sure your function uses try-except to handle the situation where the file to copy doesn't exist. It should tell the user something like:
Couldn't open the file: sadfsf.Txt
Output file NOT written.
I know I haven't written a handle for the error thats another thing I am stuck with too
import sys
print(" *** Truncating File Copy ***")
infile_name = input("Please enter the name of the file to copy: ")
outfile_name = input("Please enter the name of the new copy: ")
infile = open(infile_name, 'r', errors = 'ignore')
outfile = open(outfile_name, 'w')
def omit():
omitline = int(input("Omit how many lines from the start:"))
for num in range (omitline):
outfile.write(' ')
infile = open(infile_name, 'r', errors = 'ignore')
outfile = open(outfile_name, 'w')
omit()
outfile.write(infile.read())
infile.close()
outfile.close()
Posts: 3,458
Threads: 101
Joined: Sep 2016
Get rid of lines 5 and 6. They do nothing, but open file handles that you never deal with.
I'd suggest massively simplifying your task, and just using strings instead of files. Once it works, then you can add in file handling.
Something like:
in_lines = '''There once was a time bygone,
Way back in computing’s dawn.
The language was Lisp,
Its functions were crisp,
But its parentheses went on and on and on.
There once was a language named Perl,
That Larry Wall thought would take over the world.
But the syntax was homely,
And often write-only,
And reading your code would make you hurl.
Then came the wind to our sails,
In a language that every hipster hails.
Its name was Ruby,
It made an expert from a newbie,
Until it got traffic and it fell off the rails.'''.split("\n")
skip_first_lines = 3
skip_last_lines = 5
out_lines = []
# build the output
# next steps:
# get input from a file
# save output to a file
# get skip numbers from user
Posts: 53
Threads: 14
Joined: Mar 2017
(May-12-2017, 03:49 AM)nilamo Wrote: Get rid of lines 5 and 6. They do nothing, but open file handles that you never deal with.
I'd suggest massively simplifying your task, and just using strings instead of files. Once it works, then you can add in file handling.
Something like:
in_lines = '''There once was a time bygone,
Way back in computing’s dawn.
The language was Lisp,
Its functions were crisp,
But its parentheses went on and on and on.
There once was a language named Perl,
That Larry Wall thought would take over the world.
But the syntax was homely,
And often write-only,
And reading your code would make you hurl.
Then came the wind to our sails,
In a language that every hipster hails.
Its name was Ruby,
It made an expert from a newbie,
Until it got traffic and it fell off the rails.'''.split("\n")
skip_first_lines = 3
skip_last_lines = 5
out_lines = []
# build the output
# next steps:
# get input from a file
# save output to a file
# get skip numbers from user So would turning the text files text into a list and then using something similar to that code work?
Posts: 3,458
Threads: 101
Joined: Sep 2016
Define "work". My code does nothing, so it won't solve your problem. But simplifying what you're doing could help you solve your issues.
Programming is all about breaking your problems down into very small pieces, and building upon those pieces. Trying to solve all of the problem all at once is much more complicated.
Posts: 53
Threads: 14
Joined: Mar 2017
Posts: 53
Threads: 14
Joined: Mar 2017
May-13-2017, 11:12 AM
(This post was last modified: May-13-2017, 03:47 PM by buran.)
Does anyone know how to take user inputs and put that in to the slice method?. I know there are two numbers in the slice method separated by colon. The one on the left will remove elements on the left of the list. The number on the right will remove elements from the end the list.
Something like
sliceline = int(input("How many lines from the start")
for num in range (sliceline):
list[sliceline:] and then the same for the end slicing number
sliceline2 = int(input("How many lines from the start")
for num in range (sliceline):
list[:sliceline2] Hard to explain sorry but yeah im pretty stuck.
Posts: 4,220
Threads: 97
Joined: Sep 2016
You've pretty much got right there. If you've just got one list (and please don't name it list), then you can eliminate the for loops and it should work. Although, the second question should be "... from the end".
Posts: 53
Threads: 14
Joined: Mar 2017
(May-13-2017, 11:50 AM)ichabod801 Wrote: You've pretty much got right there. If you've just got one list (and please don't name it list), then you can eliminate the for loops and it should work. Although, the second question should be "... from the end".
Hmm ok then I really am not sure why this won't work... I basically need to do exactly what I was saying in the first post but for a text file
here is the code I have but the output just stays the same as the old file when copied to a new file no changes
import sys
print(" *** Truncating File Copy ***")
infile_name = input("Please enter the name of the file to copy: ")
outfile_name = input("Please enter the name of the new copy: ")
infile = open(infile_name, 'r', errors = 'ignore')
outfile = open(outfile_name, 'w')
file = open (infile_name, 'r', errors = 'ignore')
newList = file.readlines()
def omit():
omitline = int(input("Omit how many lines from the start:"))
for num in range (omitline):
newList[omitline:]
def omit2():
omitline2 = int(input("Omit how many lines from the end:"))
for num in range (omitline2):
newList[:omitline2]
infile = open(infile_name, 'r', errors = 'ignore')
outfile = open(outfile_name, 'w')
omit()
omit2()
outfile.write(infile.read())
infile.close()
outfile.close()
|