简体   繁体   中英

Writing numbers to the second line of txt file wtih adding number on it in Python

I have a script that writes the number at the end of request.url but I need to write it to first line of txt then I want to write another number to the second line of this txt by adding 50000 to the number

Here is my script:

f = open('numbers.txt', 'w')
            f.write(urlparse(request.url).path.split('/')[-1])

An example txt file that I want to make:

6100098 <<< this is written from request.url
6150098 <<< this must be written from adding 50000 more

This might work if you need only those 2 number. Also I recommend you to add additional checks to make sure you got the correct value from the request.url.

with open('numbers.txt', 'w') as f:
    initial_value = urlparse(request.url).path.split('/')[-1]
    f.write(initial_value + '\n')
    f.write(str(int(initial_value) + 50000))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM