Python Forum

Full Version: Unable to read csv file inside full code snippet, at the same time its working seprat
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here I used GetSatIDValue function for reading created csv file from getfilelist function. But its not happened as expected. At the same time this code can able to read csv file, when its using separately. Please have a look on below code.

So I tried like this. Ran code entirely, then used code under GetSatIDValue function separately.That works fine.
import os,subprocess,ogr
import time,csv
import csv,re
import tempfile,argparse
class Stringchecker(object):
def run(self):
    textfile="file.csv"
    returnedcsvops=self.csvops(textfile)
    returenedgetfilelist=self.getfilelist(returnedcsvops[1])
    returnedGetSatIDValue=self.GetSatIDValue(returenedgetfilelist)

def csvops(self,textfile):
    csvfileOp=open(textfile,'rb')
    csvfileRd=csv.reader(csvfileOp)
    for row in csvfileRd:
        satname.append(row[0])
        textfilepath.append(row[1])


    return [satname,textfilepath]
def getfilelist(self,passedTextfilePath):


    tmpdir=tempfile.gettempdir()#getting system temp directory
    presentTimeforDir=time.time()
    stringOftime=(str(presentTimeforDir)).replace(".","")
    print "stringOftime",stringOftime
    fileListDir=os.path.join(tmpdir,"filelist",stringOftime)# Creating temp file with timestamp
    #fileListDir1=os.path.join(fileListDir1,stringOftime)
    os.makedirs(fileListDir) # making directory

    print "created Directory",fileListDir
    for i in passedTextfilePath:#getting parrent file for all text files and that will be save in listParentTextFile
        listParentTextFile.append(os.path.dirname(i))

    os.chdir(fileListDir)

    for i in range(0,len(listParentTextFile)):

        x=0
        le=len(listParentTextFile)+1
        while x<le:
            print "input check",str(listParentTextFile[i])
            print "---"                                       
            tempvar=subprocess.Popen([" ls "+ str(listParentTextFile[i])+">"+str(x)+".csv"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)


            if x==len(listParentTextFile):
                break
            x=x+1




    csvList=os.listdir(fileListDir)



    print "csvList",csvList
    os.getcwd()
    return csvList
def GetSatIDValue(self,filelistPath):


    d=os.getcwd()

    for i in filelistPath:
        sdirectories.append(os.path.join(d,i))


    for i in range(0,len(sdirectories)):
        with open(sdirectories[i], "rb") as f_obj:
            print "f_obj",f_obj
            spamreader=list(csv.reader(f_obj))
            print "CREATE..........",list(spamreader)
            MULfolders.append(spamreader)
    print MULfolders


    return MULfolders


if __name__ == '__main__':

#Make an object for class
   obj=Stringchecker()
 #declaring variable
   MULfolders=[] 
   sdirectories=[]
   spamreader=[] 
   lis=[]
   satname=[]
   textfilepath=[]
   listParentTextFile=[]
   list1=[]
   csvList=[]
   data=[]
   openfile=[]
   obj.run()
Please post your code in Python code tags and potential errors in error tags (full traceback message). You can find help here.
In this function I am facing issue , at the same time its working fine whenever i am using separately but not along with full script.

def GetSatIDValue(self,filelistPath):


d=os.getcwd()

for i in filelistPath:
sdirectories.append(os.path.join(d,i))


for i in range(0,len(sdirectories)):

# this is the error part. Its not reading the created CSV file.

with open(sdirectories[i], "rb") as f_obj:
print "f_obj",f_obj
spamreader=list(csv.reader(f_obj))
print "CREATE..........",list(spamreader)
MULfolders.append(spamreader)
print MULfolders