0

How to parse the text file in python and read the lines which contains URLs of git repos without using any libraries?

Note: The text file contains 10 URLs of git repositories.

Format of text file:

repo_name https://www.github.com/path/to/repo
repo_name https://www.github.com/path/to/repo
repo_name https://www.github.com/path/to/repo

Edit: Got it sorted.

def main():
    # reading txt file
    opFile = open("testrepo/textfile.txt", "r")
    lines = opFile.readlines()
    opFile.close()

    for line in lines:
        line = line.strip()
        parts = line.split()
        repo_name = parts[0]
        branch = parts[1]
        git_url = parts[2]
        print("Repository name: {} \n Branch name: {} \n Git clone url: {}".format(repo_name, branch, git_url))

if __name__ == "__main__":
    main()

Output:

Repository name: repo_name
Branch name: master 
Git clone url: https://gitlab.com/path/to/repo.git
Dhruvik
  • 33
  • 1
  • 5
  • 2
    Possible duplicate of [Python way to clone a git repository](https://stackoverflow.com/questions/2472552/python-way-to-clone-a-git-repository) – Shanteshwar Inde Nov 12 '19 at 06:48
  • @ShanteshwarInde I don't want to use libraries. The text file contains 10 git repos url to clone. the only way is to parse the file but I don't know how to – Dhruvik Nov 12 '19 at 06:53
  • Then please mention all things in question. how we come to know what you're looking for? – Shanteshwar Inde Nov 12 '19 at 06:56
  • @ShanteshwarInde I have modified the question. – Dhruvik Nov 12 '19 at 06:59
  • Did you try anything? Like select the appropriate strings with sth like `if 'www.github.com' in string:...`? – FObersteiner Nov 12 '19 at 07:09
  • @MrFuppes No I have not tried this method but I think it wont work for multiple lines because 'www.github.com' would be common in each line. – Dhruvik Nov 12 '19 at 07:17
  • ok, that wasn't clear in the former version of your question... also, the question is not really related to cloning git repos, as it stands, this is just about txt file parsing - in case you wonder where the dupe flags come from. – FObersteiner Nov 12 '19 at 07:41
  • @MrFuppes It was. I had given the format of text file – Dhruvik Nov 12 '19 at 08:49

1 Answers1

0

your file is just 2 part:

repo_name ==> [0]
https://www.github.com/path/to/repo ==> [1]

So you will give an error:

IndexError: list index out of range