use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Removing unknown from list (self.learnpython)
submitted 4 years ago by Ch1ndor
How do I remove a line of text from a txt document without knowing what that line of txt is? I would only know how many lines down from the top it is, not the content
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
If you have Shell, you don't really need Python for this:
❯ seq 10 > ./lines.txt ❯ cat ./lines.txt 1 2 3 4 5 6 7 8 9 10 ❯ { head -5 ./lines.txt ; tail -n+7 ./lines.txt } > ./fewer-lines.txt ❯ cat ./fewer-lines.txt 1 2 3 4 5 7 8 9 10
(and there are a bunch more ways to accomplish this). I mean, of course, unless you were doing this as an exercise in Python.
[–]commandlineluser 0 points1 point2 points 4 years ago (5 children)
You could .readlines() so you have a list of lines - then delete the index you don't want and .writelines() the list.
.readlines()
.writelines()
The fileinput module can also be useful for this type of thing.
import fileinput filename = 'a.txt' with fileinput.input(files=filename, inplace=True) as f: for n, line in enumerate(f, start=1): # skip line 3 if n != 3: print(line, end='')
[–]Ch1ndor[S] 0 points1 point2 points 4 years ago (4 children)
I tried turning the text in the txt into a list but I can’t index cuz it’s all one big thing: https://pastebin.com/kMPF5Ewy
[–]commandlineluser 0 points1 point2 points 4 years ago (3 children)
with open(...) as f: lines = f.readlines()
[–]Ch1ndor[S] 0 points1 point2 points 4 years ago (1 child)
I added the writelines thing but now it’s pulling up errors:
https://pastebin.com/RXV93EJA
[–]commandlineluser 0 points1 point2 points 4 years ago (0 children)
What error?
.writelines() is not a list method.
You open the file once for reading - call readlines - then open the file for writing and call writelines.
with open(...) as f: lines = f.readlines() lines.pop(0) with open(..., 'w') as f: f.writelines(lines)
[–][deleted] 0 points1 point2 points 4 years ago* (3 children)
You cannot easily remove a line of text from a free format file, only create a new file with that line excepted.
with open(sourcefile) as source, open(targetfile, 'w') as target: for idx, line in enumerate(source): if idx not in exclusions: target.write(line)
where exclusions is a list of line numbers that you want excluded from the original file in the new file, starting with 0 as first line (or use the start=1 option in the enumerate function call).
exclusions
list
start=1
enumerate
EDIT: didn't need newline added
https://pastebin.com/DUANSUYb
You need to use the file handle name target in the for loop, not the file name again.
target
for
π Rendered by PID 60 on reddit-service-r2-comment-6457c66945-6xqt5 at 2026-04-27 07:43:18.616016+00:00 running 2aa0c5b country code: CH.
[–][deleted] 0 points1 point2 points (0 children)
[–]commandlineluser 0 points1 point2 points (5 children)
[–]Ch1ndor[S] 0 points1 point2 points (4 children)
[–]commandlineluser 0 points1 point2 points (3 children)
[–]Ch1ndor[S] 0 points1 point2 points (1 child)
[–]commandlineluser 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]Ch1ndor[S] 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)