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
  problem in output of a snippet code akbarza 2 385 Feb-28-2024, 07:15 PM
Last Post: deanhystad
  Unable to understand the meaning of the line of code. jahuja73 0 310 Jan-23-2024, 05:09 AM
Last Post: jahuja73
  Recommended way to read/create PDF file? Winfried 3 2,901 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,470 Nov-09-2023, 10:56 AM
Last Post: mg24
  Navigating file directories and paths inside Jupyter Notebook Mark17 5 718 Oct-29-2023, 12:40 PM
Last Post: Mark17
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 971 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 897 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  read file txt on my pc to telegram bot api Tupa 0 1,129 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 1,125 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 1,298 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

Forum Jump:

User Panel Messages

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