Python Forum

Full Version: How to put together datas from different files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am dealing with a program, which puts together some datas from different files. I wrote a part of the code it looks like:


for path, subdirs, files in os.walk(r'./'):
    for filename in files:
        if filename.startswith("mli_value_IHAR"):
            f = os.path.join(path, filename)
            date = (f[-12:-4])
            myfile = open('test.txt', 'a')
            myfile.write(date + "\n")
            myfile.close()

But I also wold like to insert a part to the code, which puts each row - so next to the dates - an intesity value, that would come from a file called mli_value_IHAR_date. This file looks contains a number, that I want to convert to a real number (405328.0):
2624 3127 4.05328e+05

So finally it would look like fowexample 20180101 405328.0.
Can anyone help me how to solve this problem?
Thanks
mli_file = open("mli_value_IHAR_date", "r") 	
data = mli_file.readline().split(" ")

val = str(float(data[2]))

myfile.write(date + " " + val + "\n")
And your next step in learning Python is formatting output https://pyformat.info/
I have one more question. How can I skip to get an ,,ValueError: could not convert string to float:" if there is no data in the text file?