Jump to content

I'm writing python how do I get a specific line from a specific file?

Poet129
Go to solution Solved by Sauron,
6 minutes ago, Poet129 said:

How do I get it line by line?

https://www.geeksforgeeks.org/read-a-file-line-by-line-in-python/

I know how to open files, write to them, and more but for the life of me I can't figure this out. Please help.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Assuming you know what you're looking for, you'll read the file line by line and set a condition

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Slottr said:

Assuming you know what you're looking for, you'll read the file line by line and set a condition

Yes but how do I read a specific line instead of the whole thing?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Poet129 said:

Yes but how do I read a specific line instead of the whole thing?

You won't, you'll go by it line by line 

 

You can for loop over it and set a condition depending on the line

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Slottr said:

You won't, you'll go by it line by line 

 

You can for loop over it and set a condition depending on the line

How do I get it line by line?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Slottr said:

For loop

Ok but how do I read the file line by line in the for loop? I can't just filecontent.read().

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Poet129 said:

Ok but how do I read the file line by line in the for loop? I can't just filecontent.read().

If you already have the file open, you just need to loop through it as in 

 

For i in File: 

 

Do stuff to line 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Slottr said:

If you already have the file open, you just need to loop through it as in 

 

For i in File: 

 

Do stuff to line 

Ok but How do i know what line I'm on?

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Poet129 said:

Ok but How do i know what line I'm on?

Use a counter

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×