Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to .txt file
#1
Hi,
How do I write a piece of text like this into a .txt file?:

Jan:Kees:Piet:klaas
Kees:Kees:Piet:klaas
Klaas:Kees:Piet:klaas
Piet:Kees:Piet:klaas
Kees:Kees:Piet:klaas
Jan:jan:Piet:klaas
Jan:Kees:piet:klaas
Jan:henk:Piet:klaas
Jan:Kees:Piet:klaas

my current code looks like this:
proxies = input('proxies: ')
with open('Proxies.txt', 'r+') as file:
    file.write(proxies)
my input for proxies will be that list i've shown above. But when I run this code it doesn't write anything down in the file. Does somebody know how to do this?
Reply
#2
You're missing a comma on line 2 between "Proxies.txt" and "r+"
Reply
#3
yeah found that out earlier on, but still doesnt work
Reply
#4
Need loop to write that,add new line(\n) when write to file and way to quit.
with open('Proxies.txt', 'a') as f:
    while True:
        proxies = proxies = input('proxies,<q> to quit: ')
        if proxies == 'q':
            break
        else:
            print(proxies)
            f.write(f'{proxies}\n')
Reply
#5
i want it to be able to write everything in once, not line by line
Reply
#6
Can do that and can eg use white space as delimiter.
with open('Proxies.txt', 'a') as f:
    prox_text = ""
    proxies = input('proxies: ')
    prox_text += proxies
    prox_text = prox_text.split()
    f.write("\n".join(prox_text))
Test:
Output:
E:\div_code λ python pro_all.py proxies: Jan:Kees:Piet:klaas Jan:Kees:Piet:klaas Jan:Kees:Piet:klaas E:\div_code λ cat Proxies.txt Jan:Kees:Piet:klaas Jan:Kees:Piet:klaas Jan:Kees:Piet:klaas
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,396 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,017 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to External File DaveG 9 2,525 Mar-30-2022, 06:25 AM
Last Post: bowlofred
  Writing to file ends incorrectly project_science 4 2,708 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Writing unit test results into a text file ateestructural 3 4,777 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,450 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 3,832 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,385 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Writing to File Issue Flash_Stang 3 2,540 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  Help! Formatting and Writing to a File bwdu 2 2,423 Apr-19-2020, 09:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020