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

What is the simplest way to print a specific version number in a file?

I’m trying to print a specific version number in a file. In this case, it should print '7.6.3.23091805'.

I know basic file management in Python, but I can’t seem to run through the string or list to do this.

Here’s the text file:

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

branch = u'fix'
nightly = False
official = True
version = u'7.6.3.23091805'

And here’s what I tried:

version_file = open("/version.py")
version_file_list = []
for line in version_file:
    split_line = line.split("'")
    version_file_list.extend(split_line)
version_file.close()

print(version_file_list)

I can’t seem to get past that, and I’m having trouble understanding string methods.

>Solution :

If you want to read the python file as a plain text file, you can do the following.

lines = []
with open("version.py", "r") as f:
    lines = f.readlines()

for line in lines:
    tokens = line.strip().split()
    if tokens[0] == "version":
        print(tokens[-1])

But, a better way to do this would be to simply import the variable from the python file.

from version import version

print(version)
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