Saltar al contenido

~ / archivo / stackoverflow / 50418720

programme complies without printing results in python

StackOverflow · python · ayudé
  1. programming pregunta -3 2018-05-18 20:19

    Whenever I compile this program it doesn't show me any print result. Can someone please tell me whats wrong is with this.Secondly I want to know that genfrom txt command is dealing with the data written in the file or is it doing something with the name of the file that is check1.0 and check1.5.

    `path="folder"
    datafiles=os.listdir(path)
    prefix='check'  
    dicdata={}
    for df in datafiles:
        if re.match(prefix,df) and os.path.isfile(df):
            print ("df values are %s" % df)
    
            hmax=locale.atof(df[5:])   
            print ("hmax values are %s"% hmax)
            data=genfromtxt(df, delimiter=', ')
            print data
            dicdata[hmax]=data
            print dictData
           print len(data[0])
    
    • OneCricketeer 2018-05-18 20:34 We cannot see what datafiles gives or your genfromtxt function... Try printing anything outside of the for loop
    • tdelaney 2018-05-18 20:51 print(df) in the loop and you'll see its just the file name, not the path.
    • Miguel Ortiz yo 2018-05-19 23:19 Include the function genfromtxt.
  2. Miguel Ortiz yo respuesta 2018-05-18 20:46

    The problem is the CWD (Current Working Directory) which may differ with the location of the files you're listing:

    #/usr/bin/python
    
    import os
    import re
    
    path="/home/mortiz/Documents/projects"
    datafiles=os.listdir(path)
    prefix='check'
    dicdata={}
    for df in datafiles:
    #    print df
        if re.match(prefix,df) and os.path.isfile(path+'/'+df):
     #           print ("df values are %s % df")
                print df
    

    Giving the absolute path of the file to your function in the condition os.path.isfile() , then that will return True and the code will be executed as you expect.

    os.path.isfile(path+'/'+df)
    

    genfromtxt is a function, not a command and you didn't provide it in your question, there's no way to tell you what is it doing without seeing its content.

    • tdelaney 2018-05-18 20:50 os.listdir lists all of the contents of the directory. Files, subdirectories, links, special files.
    • Miguel Ortiz yo 2018-05-18 20:52 True @tdelaney. fixing my answer because I've tested part of the script and the behaviour isn't what I've expected.
    • programming 2018-05-19 01:01 How I can check the dimensions of the data? because without knowing dimensions i can't proceed in the actual code
  3. tdelaney respuesta 2018-05-18 20:53

    os.listdir lists the contents of the directory but not the path needed to use them. You need to prepend path first. Since that changes what your regex is looking for, you can do the tests in two steps

    path="folder"
    datafiles=os.listdir(path)
    prefix='check'  
    dicdata={}
    for df in datafiles:
        if re.match(prefix,df):
            df = os.path.join(path, df)
            if os.path.isfile(df):
                print ("df values are %s" % df)
                hmax=locale.atof(df[5:])   
                print ("hmax values are %s"% hmax)
                data=genfromtxt(df, delimiter=', ')
                print data
                dicdata[hmax]=data
                print dictData
                print len(data[0])
    
    • Miguel Ortiz yo 2018-05-18 20:58 True, I've realized it after you mentioned what the function does, tested again and it was obvious :)
    • programming 2018-05-19 00:26 I have tried the above code but it is giving ValueError: could not convert string to float: r/check1.5
    • programming 2018-05-19 00:46 I have fixed the value error but when I print data it gives me [nan nan]. I want to know that will genfromtxt function show the result separating the values by , in the files named check1.0, check 1.5

← todos los hilos de StackOverflow