File Creation Not Working In A Thread Class?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bc90021

    File Creation Not Working In A Thread Class?

    Hi All,

    Thanks in advance for any and all help!

    I have this code:

    g = open(fileName, 'a')

    where fileName is defined before the line it's used in. It works fine
    when I use it outside a thread class.

    When I put the same line in a thread class, it no longer works, and I get
    an error:

    IOError: [Errno 2] no such file u'fileName'

    Are threads not allowed to create files?
  • Arnaud Delobelle

    #2
    Re: File Creation Not Working In A Thread Class?

    bc90021 <python@bc90021 .netwrites:
    Hi All,
    >
    Thanks in advance for any and all help!
    >
    I have this code:
    >
    g = open(fileName, 'a')
    >
    where fileName is defined before the line it's used in. It works fine
    when I use it outside a thread class.
    >
    When I put the same line in a thread class, it no longer works, and I get
    an error:
    >
    IOError: [Errno 2] no such file u'fileName'
    >
    It's telling you that you haven't got a file called 'fileName'.
    Posting the code that triggers this error would allow people to
    diagnose the error accurately rather than guessing.

    --
    Arnaud

    Comment

    • Gary Herron

      #3
      Re: File Creation Not Working In A Thread Class?

      bc90021 wrote:
      Hi All,
      >
      Thanks in advance for any and all help!
      >
      I have this code:
      >
      g = open(fileName, 'a')
      >
      I don't believe you. I think you have something like

      g = open('fileName' , 'a')

      instead of (as you claim)

      g = open(fileName, 'a')

      Do you see the difference?

      Develop the skill of reading the error messages *very* carefully. Your error says there is no file named "fileName", and if you think about what's on your disk, I'll bet you won't find a file whose name is "fileName".


      Gary Herron


      where fileName is defined before the line it's used in. It works fine
      when I use it outside a thread class.
      >
      When I put the same line in a thread class, it no longer works, and I get
      an error:
      >
      IOError: [Errno 2] no such file u'fileName'
      >
      Are threads not allowed to create files?
      --

      >

      Comment

      • bc90021

        #4
        Re: File Creation Not Working In A Thread Class?

        On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
        bc90021 <python@bc90021 .netwrites:
        >
        >Hi All,
        >>
        >Thanks in advance for any and all help!
        >>
        >I have this code:
        >>
        >g = open(fileName, 'a')
        >>
        >where fileName is defined before the line it's used in. It works fine
        >when I use it outside a thread class.
        >>
        >When I put the same line in a thread class, it no longer works, and I
        >get an error:
        >>
        >IOError: [Errno 2] no such file u'fileName'
        >>
        >>
        It's telling you that you haven't got a file called 'fileName'. Posting
        the code that triggers this error would allow people to diagnose the
        error accurately rather than guessing.
        f = open(otherFile) .readlines()
        for i in len(f):
        for c in range(0,24,1):
        if os.name == "posix":
        tempfileName = "\"proctemp/" + self.matrix[c][0]
        + "_tmp_" + fileName + ".txt\""
        if re.search(f[i], pattern):
        g = open(tempfileNa me, 'a')
        g.write(f[i])


        This code works *perfectly* unless I put it in a class that inherits from
        threading.Threa d. In the thread class, everything works (I can see the
        "c" value, and I can print out each line in "f[i]", it's just that the g
        = open line doesn't work.



        Comment

        • bc90021

          #5
          Re: File Creation Not Working In A Thread Class?

          On Sun, 11 May 2008 10:51:34 -0700, Gary Herron wrote:
          bc90021 wrote:
          >Hi All,
          >>
          >Thanks in advance for any and all help!
          >>
          >I have this code:
          >>
          >g = open(fileName, 'a')
          >>
          >>
          I don't believe you. I think you have something like
          >
          g = open('fileName' , 'a')
          >
          instead of (as you claim)
          >
          g = open(fileName, 'a')
          >
          Do you see the difference?
          >
          Develop the skill of reading the error messages *very* carefully. Your
          error says there is no file named "fileName", and if you think about
          what's on your disk, I'll bet you won't find a file whose name is
          "fileName".
          >
          >
          Gary Herron
          >
          Gary,

          I can assure you that that's not the case. (Of course, you're free to
          believe me or not believe me at your convenience.) You are right that I
          don't have that file on the disk - it is supposed to be created with the
          "open" line! It works *perfectly* outside the class the inherits from
          threading.Threa d; inside the threading.Threa d class it does NOT create
          the file, whether I use g = open(fileName, 'a') or g = open (fileName,
          'w'). Hence my obvious confusion.

          Comment

          • Arnaud Delobelle

            #6
            Re: File Creation Not Working In A Thread Class?

            bc90021 <python@bc90021 .netwrites:
            On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
            >
            >bc90021 <python@bc90021 .netwrites:
            >>
            >>Hi All,
            >>>
            >>Thanks in advance for any and all help!
            >>>
            >>I have this code:
            >>>
            >>g = open(fileName, 'a')
            >>>
            >>where fileName is defined before the line it's used in. It works fine
            >>when I use it outside a thread class.
            >>>
            >>When I put the same line in a thread class, it no longer works, and I
            >>get an error:
            >>>
            >>IOError: [Errno 2] no such file u'fileName'
            >>>
            >>>
            >It's telling you that you haven't got a file called 'fileName'. Posting
            >the code that triggers this error would allow people to diagnose the
            >error accurately rather than guessing.
            >
            f = open(otherFile) .readlines()
            for i in len(f):
            for c in range(0,24,1):
            if os.name == "posix":
            tempfileName = "\"proctemp/" + self.matrix[c][0]
            + "_tmp_" + fileName + ".txt\""
            if re.search(f[i], pattern):
            g = open(tempfileNa me, 'a')
            g.write(f[i])
            >
            >
            This code works *perfectly* unless I put it in a class that inherits from
            threading.Threa d. In the thread class, everything works (I can see the
            "c" value, and I can print out each line in "f[i]", it's just that the g
            = open line doesn't work.
            It's difficult to know what's wrong with the code you posted because:

            * it is not self-contained: otherFile, fileName, pattern are names
            which you do not define;

            * the IOError you reported earlier can't arise as a result of running
            this code.

            * you claim it works unless you put it in a subclass of
            threading.Threa d. Why don't you post this instead, and show us the
            traceback?

            HTH

            FWIW, my crystal ball (whose predictions I don't usually report!)
            tells me the same as Garry Herron's.

            --
            Arnaud

            Comment

            • Arnaud Delobelle

              #7
              Re: File Creation Not Working In A Thread Class?

              Arnaud Delobelle <arnodel@google mail.comwrites:
              bc90021 <python@bc90021 .netwrites:
              >
              >On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
              >>
              >>bc90021 <python@bc90021 .netwrites:
              >>>
              >>>Hi All,
              >>>>
              >>>Thanks in advance for any and all help!
              >>>>
              >>>I have this code:
              >>>>
              >>>g = open(fileName, 'a')
              >>>>
              >>>where fileName is defined before the line it's used in. It works fine
              >>>when I use it outside a thread class.
              >>>>
              >>>When I put the same line in a thread class, it no longer works, and I
              >>>get an error:
              >>>>
              >>>IOError: [Errno 2] no such file u'fileName'
              >>>>
              >>>>
              >>It's telling you that you haven't got a file called 'fileName'. Posting
              >>the code that triggers this error would allow people to diagnose the
              >>error accurately rather than guessing.
              >>
              >f = open(otherFile) .readlines()
              >for i in len(f):
              > for c in range(0,24,1):
              > if os.name == "posix":
              > tempfileName = "\"proctemp/" + self.matrix[c][0]
              >+ "_tmp_" + fileName + ".txt\""
              > if re.search(f[i], pattern):
              > g = open(tempfileNa me, 'a')
              > g.write(f[i])
              >>
              >>
              >This code works *perfectly* unless I put it in a class that inherits from
              >threading.Thre ad. In the thread class, everything works (I can see the
              >"c" value, and I can print out each line in "f[i]", it's just that the g
              >= open line doesn't work.
              >
              It's difficult to know what's wrong with the code you posted because:
              >
              * it is not self-contained: otherFile, fileName, pattern are names
              which you do not define;
              >
              * the IOError you reported earlier can't arise as a result of running
              this code.
              Correction: it can, if otherFile == u'fileName'

              So I put 1000 rupees on this being the cause of the error :)

              --
              Arnaud

              Comment

              • bc90021

                #8
                Re: File Creation Not Working In A Thread Class?

                It's difficult to know what's wrong with the code you posted because:
                >
                * it is not self-contained: otherFile, fileName, pattern are names
                which you do not define;
                >
                * the IOError you reported earlier can't arise as a result of running
                this code.
                >
                * you claim it works unless you put it in a subclass of
                threading.Threa d. Why don't you post this instead, and show us the
                traceback?
                >
                HTH
                >
                FWIW, my crystal ball (whose predictions I don't usually report!) tells
                me the same as Garry Herron's.
                Here's the thread class:

                #single file is the file we're working on, whose name is passed into the class and which does exist
                #matrix is a list of lists that contains info about the files - for this example, [c][0] contains a string, [c][2] contains true or false, and [c][3] contains a pattern to match
                #tfValue is another true or false value


                class FileProcThread( threading.Threa d):
                def __init__(self, singleFile, matrix, tfValue):
                self.singleFile = singleFile
                self.matrix = matrix
                self.tfValue = tfValue
                threading.Threa d.__init__(self )
                def run(self):
                (dirName, fileName) = os.path.split(s elf.singleFile)
                f = open(self.singl eFile).readline s()
                copying = False
                for i in range(len(f)):
                for c in range (len(self.matri x)):
                if (re.search(self .matrix[c][3], f[i])):
                if (self.matrix[c][2] == True):
                copying = True
                if os.name == "posix":
                if (self.tfValue == False):
                tempfileName = "\"proctemp/" + self.matrix[c][0] + "_tmp_" + fileName +
                ".txt\""
                else:
                tempfileName = "\"proctemp/" + self.matrix[c][0] + "_other.txt \""
                else:
                if (self.tfValue == False):
                tempfileName = "\"proctemp \\" + self.matrix[c][0] + "_tmp_" + fileName + ".txt\""
                else:
                tempfileName = "\"proctemp \\" + self.matrix[c][0] + "_other.txt \""
                else:
                copying = False
                if (re.search(self .matrix[c][4], f[i])):
                copying = False
                if (copying):
                print "We're in copying, and tempfileName is: %s...\n" % tempfileName
                #The above line correctly prints the temporary file name every time! The directory exists, too!
                g = open(tempfileNa me, 'a') #This does not work. Notice I do NOT have quotes around tempfileName, as I said.
                g.write(f[i])
                g.close()

                Like I said, this works FINE outside the thread class. I hope that the formatting comes through...

                Comment

                • 7stud

                  #9
                  Re: File Creation Not Working In A Thread Class?

                  On May 11, 12:42 pm, bc90021 <pyt...@bc90021 .netwrote:
                  It's difficult to know what's wrong with the code you posted because:
                  >
                  * it is not self-contained: otherFile, fileName, pattern are names
                    which you do not define;
                  >
                  * the IOError you reported earlier can't arise as a result of running
                    this code.
                  >
                  * you claim it works unless you put it in a subclass of
                    threading.Threa d.  Why don't you post this instead, and show us the
                    traceback?
                  >
                  HTH
                  >
                  FWIW, my crystal ball (whose predictions I don't usually report!) tells
                  me the same as Garry Herron's.
                  >
                  Here's the thread class:
                  >
                  #single file is the file we're working on, whose name is passed into the class and which does exist
                  #matrix is a list of lists that contains info about the files - for this example, [c][0] contains a string, [c][2] contains true or false, and [c][3] contains a pattern to match
                  #tfValue is another true or false value
                  >
                  class FileProcThread( threading.Threa d):
                          def __init__(self, singleFile, matrix, tfValue):
                                  self.singleFile = singleFile
                                  self.matrix = matrix
                                  self.tfValue = tfValue
                                  threading.Threa d.__init__(self )
                          def run(self):
                                  (dirName, fileName) = os.path.split(s elf..singleFile )
                                  f = open(self.singl eFile).readline s()
                                  copying = False
                                  for i in range(len(f)):
                                          for c in range (len(self.matri x)):
                                                  if (re.search(self .matrix[c][3], f[i])):
                                                          if (self.matrix[c][2] == True):
                                                                  copying = True
                                                                  if os.name == "posix":
                                                                          if (self.tfValue == False):
                                                                                  tempfileName = "\"proctemp/" + self.matrix[c][0] + "_tmp_" + fileName +
                  ".txt\""
                                                                          else:
                                                                                  tempfileName = "\"proctemp/" + self.matrix[c][0] + "_other.txt \""
                                                                  else:
                                                                          if (self.tfValue == False):
                                                                                  tempfileName = "\"proctemp \\" + self.matrix[c][0] + "_tmp_" + fileName + ".txt\""
                                                                          else:
                                                                                  tempfileName = "\"proctemp \\" + self.matrix[c][0] + "_other.txt \""
                                                          else:
                                                                  copying = False
                                                  if (re.search(self .matrix[c][4], f[i])):
                                                          copying = False
                                          if (copying):
                                                  print "We're in copying, and tempfileName is: %s...\n" % tempfileName
                                                  #The aboveline correctly prints the temporary file name every time!  The directory exists, too!
                                                  g = open(tempfileNa me, 'a')  #This does not work.  Notice I do NOT have quotes around tempfileName, as I said.
                                                  g.write(f[i])
                                                  g.close()
                  >
                  Like I said, this works FINE outside the thread class.  I hope that the formatting comes through...
                  ...and the exact error message was?

                  Here is a tip: if you want people to help you, then you have to help
                  them to help you. Personally, I wouldn't respond to anymore of your
                  questions because you seem incapable of posting the information that
                  was requested.

                  Comment

                  • bc90021

                    #10
                    Re: File Creation Not Working In A Thread Class?

                    ...and the exact error message was?
                    >
                    Here is a tip: if you want people to help you, then you have to help
                    them to help you. Personally, I wouldn't respond to anymore of your
                    questions because you seem incapable of posting the information that was
                    requested.
                    So far, the people who have answered this post have gone on the
                    assumption that I'm stupid. I'm not. I took perfectly working code,
                    cut it from one class, and put it in another. It stopped working in the
                    second class. I've spent days on this and trust me, given what I've
                    experienced of the Python community so far, if I didn't have to ask, I
                    wouldn't.

                    (I really must say that so far the help I am getting in the Python
                    community is a big let down. Whether it's on IRC or here, everyone has
                    an arrogance that I don't find anywhere else in the open source
                    community, and it seriously makes me question the choice of language that
                    I've made.)

                    The error message was at the top of the thread (am I incapable of posting
                    it, or are you incapable of following a thread?), but here it is again:

                    IOError: [Errno 2] no such file u'tempfileName'

                    Comment

                    • 7stud

                      #11
                      Re: File Creation Not Working In A Thread Class?

                      bc90021 wrote:
                      Hi All,
                      >
                      Thanks in advance for any and all help!
                      >
                      I have this code:
                      >
                      g = open(fileName, 'a')
                      >
                      where fileName is defined before the line it's used in. It works fine
                      when I use it outside a thread class.
                      >
                      When I put the same line in a thread class, it no longer works, and I get
                      an error:
                      >
                      IOError: [Errno 2] no such file u'fileName'
                      >
                      Are threads not allowed to create files?
                      ....oh yeah:

                      import threading
                      import time

                      fname = "data.txt"
                      f = open(fname)
                      print f.read()
                      f.close()

                      f = open(fname, "a")
                      f.write("some text\n")
                      f.close()

                      f = open(fname)
                      print f.read()
                      f.close()


                      class MyThread(thread ing.Thread):
                      def __init__(self, file_name):
                      threading.Threa d.__init__(self )

                      def run(self):
                      time.sleep(3)

                      f = open(fname)
                      print f.read()
                      f.close()

                      f = open(fname, "a")
                      f.write("other text\n")
                      f.close()

                      f = open(fname)
                      print f.read()
                      f.close()


                      my_t = MyThread(fname)
                      my_t.start()
                      my_t.join()


                      --output:--
                      hello
                      world

                      hello
                      world
                      some text

                      hello
                      world
                      some text

                      hello
                      world
                      some text
                      other text

                      Comment

                      • Arnaud Delobelle

                        #12
                        Re: File Creation Not Working In A Thread Class?

                        bc90021 <python@bc90021 .netwrites:
                        >...and the exact error message was?
                        >>
                        >Here is a tip: if you want people to help you, then you have to help
                        >them to help you. Personally, I wouldn't respond to anymore of your
                        >questions because you seem incapable of posting the information that was
                        >requested.
                        >
                        So far, the people who have answered this post have gone on the
                        assumption that I'm stupid. I'm not. I took perfectly working code,
                        cut it from one class, and put it in another. It stopped working in the
                        second class. I've spent days on this and trust me, given what I've
                        experienced of the Python community so far, if I didn't have to ask, I
                        wouldn't.
                        I have in no way assumed that you are stupid. I have tried to help
                        you formulate your problem better so that people on the list can help
                        you. I believe I have done so respectfully, with the aim of
                        introducing you to the modus operandi of this group.
                        (I really must say that so far the help I am getting in the Python
                        community is a big let down. Whether it's on IRC or here, everyone has
                        an arrogance that I don't find anywhere else in the open source
                        community, and it seriously makes me question the choice of language that
                        I've made.)
                        Don't judge too quickly. I think this newsgroup is on the whole
                        extremely helpful. I have learnt a lot from it. But you have to get
                        used to its ways, and until you are familiar with them, approach it
                        with humility.
                        The error message was at the top of the thread (am I incapable of posting
                        it, or are you incapable of following a thread?), but here it is again:
                        >
                        IOError: [Errno 2] no such file u'tempfileName'
                        This is different from the error message that you posted in your
                        original message.

                        Anyway, what is useful to us is a full traceback, no just an error
                        message.

                        --
                        Arnaud

                        Comment

                        • Duncan Booth

                          #13
                          Re: File Creation Not Working In A Thread Class?

                          bc90021 <python@bc90021 .netwrote:
                          The error message was at the top of the thread (am I incapable of
                          posting it, or are you incapable of following a thread?), but here it
                          is again:
                          >
                          IOError: [Errno 2] no such file u'tempfileName'
                          So which was it? At the top of the thread you said it was:

                          IOError: [Errno 2] no such file u'fileName'

                          How about posting the exact error message you got, including all of the
                          traceback and the complete original code: even when you posted "Here's the
                          thread class" you actually posted a modified version of your thread class
                          (with sarky comments added even if nothing else was changed).

                          If you post complete code and the full traceback then you'll probably get a
                          quick and accurate response telling you what the problem is. If you
                          continue to paraphrase and post bits and pieces you'll continue to get
                          random guesses.

                          Comment

                          • Duncan Booth

                            #14
                            Re: File Creation Not Working In A Thread Class?

                            7stud <bbxx789_05ss@y ahoo.comwrote:
                            >                                   
                                                        tempfileName = "\"proctemp \\" +
                            self.matrix[c][0] + "_other.txt \""
                            It wouldn't exactly result in either of the error messages you posted, but
                            I expect the spurious quote marks round the filename will be giving you
                            problems.

                            Surely you want the filename to be something like 'proctemp\fred_ other.txt'
                            rather than '"proctemp\fred _other.txt"' with the spurious double quotes?

                            Comment

                            • 7stud

                              #15
                              Re: File Creation Not Working In A Thread Class?

                              On May 11, 1:28 pm, bc90021 <pyt...@bc90021 .netwrote:
                              ...and the exact error message was?
                              >
                              Here is a tip: if you want people to help you, then you have to help
                              them to help you.  Personally, I wouldn't respond to anymore of your
                              questions because you seem incapable of posting the information that was
                              requested.
                              >
                              So far, the people who have answered this post have gone on the
                              assumption that I'm stupid.  I'm not.  I took perfectly working code,
                              cut it from one class, and put it in another.  It stopped working in the
                              second class.  I've spent days on this and trust me, given what I've
                              experienced of the Python community so far, if I didn't have to ask, I
                              wouldn't.
                              >
                              (I really must say that so far the help I am getting in the Python
                              community is a big let down.  Whether it's on IRC or here, everyone has
                              an arrogance that I don't find anywhere else in the open source
                              community, and it seriously makes me question the choice of language that
                              I've made.)
                              >
                              The error message was at the top of the thread (am I incapable of posting
                              it, or are you incapable of following a thread?), but here it is again:
                              >
                              IOError: [Errno 2] no such file u'tempfileName'
                              Well, it appears to me that this error message is different than the
                              one in your first post. But maybe I'm on LSD right now and things
                              will be appear differently tomorrow.

                              In addition, I've never seen a python error message that doesn't
                              include the traceback, which you were asked to post, but apparently
                              are still incapbable of doing. Also, any line numbers in the error
                              message should be marked in your code with comments. That will help
                              other people help you, remember?



                              Comment

                              Working...