[Tutor] [EXTERNAL]Re: Using 'with open' statementfor reading a file two lines at a time
Olsen, Avalow Y
Yuanyuan.A.Olsen at HealthPartners.Com
Thu Nov 26 21:35:45 EST 2020
Could Something like this work? I didn’t test it out. More like pseudo code...
Handle1= open('file1', 'r')
Handle2 = open('file2', 'w')
Count= 0
Lst = []
For line in handle1:
Count = count+1
Lst.append(line)
if count%2 = 0:
handle2.write(lst)
lst = []
file1 = open(filename1, "r")
file2 = open(filename2, "w")
with open(filename1) as file1:
line1 = file1.readline().strip()
line2 = file1.readline().strip()
line3 = (line1 + line2)
line4 = (line3[ 0: 4] + line3[ 4: 6] + line3[ 6: 8] + line3[ 8: 9]
+ line3[ 9:12] + line3[20:21] + line3[21:24])
file2.write(line4)
file1.close()
file2.close()
-----Original Message-----
From: Tutor [mailto:tutor-bounces+yuanyuan.a.olsen=healthpartners.com at python.org] On Behalf Of Ken Green
Sent: Thursday, November 26, 2020 7:33 PM
To: tutor at python.org
Subject: [EXTERNAL]Re: [Tutor] Using 'with open' statementfor reading a file two lines at a time
External Email: Don't click links or attachments unless you trust the email.
On 11/26/20 8:00 PM, Alan Gauld via Tutor wrote:
> On 27/11/2020 00:37, Ken Green wrote:
>
>> I am using 'with open' statement to read a file two lines at a time
>> and then combining them together to print the results into another file.
> Please don't just describe your code and think we can guess what
> you've done.
>
> Post the actual code and any actual error messages in full.
>
> There must be at least half a dozen ways to do what you describe.
> We can't fix what we can't see.
>
>> How can I continue reading the third and fourth lines and so forth on
>> until the end of the file?
> You need to use a loop, either a 'while' or 'for'
> But without seeing what you've done so far I can't say more than that.
>
>> (2020-11-5) as an interface. I used to use Geany but discovered the
>> latest version doesn't work in Ubuntu 20.04 and it only applicable
>> for Windows and Apple and there is no Linux version according to its website.
> I'm pretty sure there is a Linux version, in fact I just checked and
> v1.37 has just been released on Linux as well as Windoze and MacOS.
> v1.36 is available in the software tool for my Linux Mint 20 which is
> based on Ubuntu 20 so you should definitely be able to get it working.
>
> But if not there are a zillion other code editors out there that will
> do just fine!
I humbly apologized Alan. Below is the code:
# Daily Three Pick Three 99 Combine Test py # 11/26/2020 import sys
filename1 = "Pick_Three_Drawings_File.txt"
filename2 = "Pick_Three_Drawings_File_Combine.txt"
file1 = open(filename1, "r")
file2 = open(filename2, "w")
with open(filename1) as file1:
line1 = file1.readline().strip()
line2 = file1.readline().strip()
line3 = (line1 + line2)
line4 = (line3[ 0: 4] + line3[ 4: 6] + line3[ 6: 8] + line3[ 8: 9]
+ line3[ 9:12] + line3[20:21] + line3[21:24])
file2.write(line4)
file1.close()
file2.close()
_______________________________________________
Tutor maillist - Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
________________________________
This e-mail and any files transmitted with it are confidential and are intended solely for the use of the individual or entity to whom they are addressed. If you are not the intended recipient or the individual responsible for delivering the e-mail to the intended recipient, please be advised that you have received this e-mail in error and that any use, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited.
If you have received this communication in error, please return it to the sender immediately and delete the original message and any copy of it from your computer system. If you have any questions concerning this message, please contact the sender. Disclaimer R001.0
More information about the Tutor
mailing list