473,786 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?
Jun 27 '08 #1
23 1680
On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
bc90021 <py****@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.

Jun 27 '08 #2
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.
Jun 27 '08 #3
bc90021 <py****@bc90021 .netwrites:
On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
>bc90021 <py****@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
Jun 27 '08 #4
Arnaud Delobelle <ar*****@google mail.comwrites:
bc90021 <py****@bc90021 .netwrites:
>On Sun, 11 May 2008 18:36:25 +0100, Arnaud Delobelle wrote:
>>bc90021 <py****@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
Jun 27 '08 #5
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...
Jun 27 '08 #6
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.
Jun 27 '08 #7
...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'

Jun 27 '08 #8
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
Jun 27 '08 #9
bc90021 <py****@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
Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
11546
by: Eric | last post by:
Problem: -------- I'm trying to create an executable jar file. I can do so as long as I don't use the "package" statement in my source code. Once I put the package statement in I can't execute the jar file. Here is the criteria of what I am working with: ----------------------------------------------- Working directory: ------------------
5
2215
by: hpy_awad | last post by:
Can any body send to me a missing file to my compiler Turbo C++ ver 3,called tv.lib
8
26719
by: Gabe Moothart | last post by:
Hi, I'm writing a windows service which interacts with a separate process. Basically, it calls a process which creates a file, and then my service reads that file. The problem is, the external process can take a second or two to finish writing the file. If I try to read the file to soon, I get an exception that "The process cannot access the file because it is being used by another process". I could just set a timer, but the time it...
18
4353
by: Jen | last post by:
I'm using Microsoft's own VB.NET FTP Example: http://support.microsoft.com/default.aspx?scid=kb;en-us;832679 I can get the program to create directories, change directories, etc., but I can't get it to upload a file to the FTP server. I just get a "Cannot connect to remote server" error after this TRY: s = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
15
2837
by: David Thielen | last post by:
Hi; My ASP.NET app (C# calling J# under .net 2.0) creates a png file in a subdirectory to display as part of the created page. However, the bitmap will not display due to a security violation. Everything is the default settings I believe. IIS is running under Local System. In IIS the DefaultAppPool is running under Network Service. Annonymous access uses the account IUSR_JASMINE (machine name is Jasmine).
17
8029
by: Peter Duniho | last post by:
I searched using Google, on the web and in the newsgroups, and found nothing on this topic. Hopefully that means I just don't understand what I'm supposed to be doing here. :) The problem: I am trying to use the SaveFileDialog class to get a filename, which is subsequently opened for writing (write access, read sharing, but using read/write sharing doesn't make the problem go away anyway). Sometimes, on the statement where I...
3
4503
by: Sean C. | last post by:
Hey All, I'm having a little problem here. I have a project that I'm working on that involves a MySQL server database backend. I'm having no problem creating the database on the fly if it doesn't already exist and using it once it's created. My problem comes into play when I run the program for the first time. I'm wanting to have some kind of flag that lets me know that the database has not yet been created, so that I can call my...
9
3014
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all of the above after the file is loaded into the RichTextBox, and I am trying to speed the process up by doing it in a temp file.
7
2833
by: =?Utf-8?B?QU9UWCBTYW4gQW50b25pbw==?= | last post by:
Hi, I have been using the code (some of it has been removed for simplicity) below to allow authenticated (using ASP.NET membership database) users to get a file from their archive area. It seems to work fine, however I noticed that no web log entry is added when a successful download occurs (normally a 200 HTTP status code, however, if there is an authorization failure, it gets logged). I have a logging routine that logs a successful...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.