Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Creating a list from a text file

I have a text file called my_urls.txt that has 3 URLs:

example1.com
example2.com
example3.com
with open("../test_data/my_urls.txt", "r") as urlFile:
    urls_list = urlFile.readline().split('\n')
    print(urls_list)


['example1.com', '']

I wonder why urls_list is not giving me ['example1.com', 'example2.com', 'example3.com']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to iterate over the open file. This iterates over the lines for you:

with open("../test_data/my_urls.txt", "r") as urlFile:
    urls_list = []
    for url in urlFile:
        urls_list.append(url.strip())
    print(urls_list)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading