Python Forum
Unable to read csv file inside full code snippet, at the same time its working seprat
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to read csv file inside full code snippet, at the same time its working seprat
#1
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()
Reply
#2
Please post your code in Python code tags and potential errors in error tags (full traceback message). You can find help here.
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code isnt working. nezercat33 1 579 Mar-14-2025, 03:45 AM
Last Post: deanhystad
  Simple code not working properly tmv 2 470 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 482 Feb-13-2025, 06:13 PM
Last Post: lminc123
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 985 Dec-19-2024, 11:57 AM
Last Post: snippsat
  my code is not working erTurko 1 626 Nov-11-2024, 08:43 AM
Last Post: buran
  Read TXT file in Pandas and save to Parquet zinho 2 1,200 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,527 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Python is unable to read file Genericgamemaker 13 3,503 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,152 May-03-2024, 07:23 AM
Last Post: Pedroski55
  problem in output of a snippet code akbarza 2 1,196 Feb-28-2024, 07:15 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020