471,348 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

IOError: file() constructor not accessible in restricted mode

I've tried to find the answer to this one already but couldn't find it in the forum.

What does IOError: file() constructor not accessible in restricted mode mean?

Here is the code that is giving me the error:

Expand|Select|Wrap|Line Numbers
  1.  
  2. def checkCal():
  3.  
  4.     found = False
  5.     for line in myfile:
  6.         data = line.split(',')
  7.         if data[2] == str(agv_O_Cal.value) and data[3] == str(agv_O_Mod.value):
  8.             agv_P1.value = int(data[5])
  9.             agv_P2.value = int(data[6])
  10.             agv_P3.value = int(data[7])
  11.             agv_WS1.value = data[9]
  12.             agv_WS2.value = data[10]
  13.             agv_WS3.value = data[11]
  14.             found = True
  15.             break
  16.  
  17.     if not found:
  18.         print getComponent().name, ": ERROR - No Cal Table found "
  19.         Log = open("C:\\V-Sim\\cdata\\Sandbox\\Oshawa\\Documentation\\Loop_Log.txt","a")
  20.         simtime = str(world.simTime.raw)
  21.         realtime = str(datetime.now())
  22.         Log.write(getComponent().name + " ERROR - No Cal Table found" +" RealTime: "+ realtime +" SimTime: "+ simtime +"\n")
  23.         Log.flush()
  24.         Log.close()
  25.  
  26.  
The actual line that is giving me the error is Line 18.

Any help would be appreciated. Thanks in advance.
Jul 5 '07 #1
18 4002
bartonc
6,596 Expert 4TB
Well, Markalon, it sounds like the error message says it all.

Please tell us your OS/Platform/logon method in order for us to track this down.

Welcome back,
Barton
Jul 5 '07 #2
Well, Markalon, it sounds like the error message says it all.

Please tell us your OS/Platform/logon method in order for us to track this down.

Welcome back,
Barton

It sounds like the error message says it all? I see. Well, that clears that up. I guess I should have just read the error message then instead of asking what it means here.

Thanks for the help.
Jul 6 '07 #3
Motoma
3,237 Expert 2GB
This is the internet and I cannot tell if you are being facetious or you really have found your solution, either way, take a look at the Python documentation for restricted mode as well as thisfriendlier explanation.
Jul 6 '07 #4
This is the internet and I cannot tell if you are being facetious or you really have found your solution, either way, take a look at the Python documentation for restricted mode as well as thisfriendlier explanation.
I wasn't being facetious. I was being sarcastic.

Thank you for the links. I'm not sure they help me understand the problem and I still haven't been able to fix it. I have the same code in other places in the script and in other scripts, and I'm not getting that error. Just in that spot, as far as I can tell.

But I'll look into it in more detail. Thanks again for the links.
Jul 6 '07 #5
bartonc
6,596 Expert 4TB
It sounds like the error message says it all? I see. Well, that clears that up. I guess I should have just read the error message then instead of asking what it means here.

Thanks for the help.
I also asked for more information from you, which you never bothered to provide:
Expand|Select|Wrap|Line Numbers
  1. Please tell us your OS/Platform/logon method in order for us to track this down.
And suggested that further assistance was available. So I don't see a need for sarcasm nor facetiousness.
Jul 6 '07 #6
I also asked for more information from you, which you never bothered to provide:
Expand|Select|Wrap|Line Numbers
  1. Please tell us your OS/Platform/logon method in order for us to track this down.
And suggested that further assistance was available. So I don't see a need for sarcasm nor facetiousness.

I put my OS/Platform/logon method in the "Tell us your OS" thread:

XP Pro
Python 2.3 and 2.5
Not sure what you mean about logon method, but I log onto my computer directly.

My apologies for the sarcasm, but as I had no idea what it meant (Still don't, really), your answer of "it says it all" seemed somewhat elitest to me. I'm still not sure what it means or how I'm getting that error.
Jul 9 '07 #7
Motoma
3,237 Expert 2GB
I put my OS/Platform/logon method in the "Tell us your OS" thread:

XP Pro
Python 2.3 and 2.5
Not sure what you mean about logon method, but I log onto my computer directly.

My apologies for the sarcasm, but as I had no idea what it meant (Still don't, really), your answer of "it says it all" seemed somewhat elitest to me. I'm still not sure what it means or how I'm getting that error.
Python is executing your script in Restricted Execution Mode. That is why you cannot perform file IO correctly.
Jul 9 '07 #8
bartonc
6,596 Expert 4TB
I put my OS/Platform/logon method in the "Tell us your OS" thread:

XP Pro
Python 2.3 and 2.5
Not sure what you mean about logon method, but I log onto my computer directly.

My apologies for the sarcasm, but as I had no idea what it meant (Still don't, really), your answer of "it says it all" seemed somewhat elitest to me. I'm still not sure what it means or how I'm getting that error.
Yep. Sorry that I missed that post, Markalon. There have been some long hard days on the boards, lately. Not to mention my current project/deadline.

I haven't really got a clue as to how you are getting into restricted mode.
The docs mention two OLD (pre 2.3) modules: rexec and Bastion modules. If you are trying to run old code that imports these modules, check out Ch. 17 and plan to remove them, if possible (or move the file IO to a non-restricted section).
Jul 9 '07 #9
Yep. Sorry that I missed that post, Markalon. There have been some long hard days on the boards, lately. Not to mention my current project/deadline.

I haven't really got a clue as to how you are getting into restricted mode.
The docs mention two OLD (pre 2.3) modules: rexec and Bastion modules. If you are trying to run old code that imports these modules, check out Ch. 17 and plan to remove them, if possible (or move the file IO to a non-restricted section).

I don't think I'm using either of those modules. At least, I'm not importing either of them.

I also have no idea how I'm getting into restricted mode. The strange thing is that it seems to work the first time (maybe first several times) that I hit that code, but then all of a sudden it starts giving me the error.

Basically, I have about 180 places that calls that file, opens it, flushes the data, then closes it. Is it possible that the error is being given to me because its trying to open it with it already open? And does it just get stuck in restricted mode?

Gah. This is quite irritating... lol.
Jul 9 '07 #10
bartonc
6,596 Expert 4TB
I don't think I'm using either of those modules. At least, I'm not importing either of them.

I also have no idea how I'm getting into restricted mode. The strange thing is that it seems to work the first time (maybe first several times) that I hit that code, but then all of a sudden it starts giving me the error.

Basically, I have about 180 places that calls that file, opens it, flushes the data, then closes it. Is it possible that the error is being given to me because its trying to open it with it already open? And does it just get stuck in restricted mode?

Gah. This is quite irritating... lol.
I just tried
Expand|Select|Wrap|Line Numbers
  1. >>> f = file('test.txt', 'w')
  2. >>> f.close()
  3. >>> f = file('test.txt')
  4. >>> f = file('test.txt')
  5. >>> f.close()
  6. >>> f.close()
  7. >>> for i in range(200):
  8. ...     f = file('test.txt')
  9. ...     
  10. >>> f.close()
but got not errors.

Is there any chance of moving to a more recent version of python?

I also tried keeping a reference to 200 instance of the same file opened sparately:
Expand|Select|Wrap|Line Numbers
  1. >>> fileList = range(200)
  2. >>> for i in range(200):
  3. ...     f = file('test.txt')
  4. ...     fileList[i] = f
  5. ...     
  6. >>> for f in fileList:
  7. ...     f.close()
  8. ...     
  9. >>> 
Jul 9 '07 #11
bartonc
6,596 Expert 4TB
I don't think I'm using either of those modules. At least, I'm not importing either of them.

I also have no idea how I'm getting into restricted mode. The strange thing is that it seems to work the first time (maybe first several times) that I hit that code, but then all of a sudden it starts giving me the error.

Basically, I have about 180 places that calls that file, opens it, flushes the data, then closes it. Is it possible that the error is being given to me because its trying to open it with it already open? And does it just get stuck in restricted mode?

Gah. This is quite irritating... lol.
I just had another thought: If you are using IDLE (F5), try running from the command-line. I've known IDLE to go off it's rocker in weird ways.
Jul 9 '07 #12
I just had another thought: If you are using IDLE (F5), try running from the command-line. I've known IDLE to go off it's rocker in weird ways.
Unfortunately, I'm not supposed to move to a different version of Python, as our software is written with 2.3 in mind. Our software engineer said we shouldn't use any other version at the moment, as he isn't sure what the effects would be.

As for IDLE, I'm only using it to edit the python code, not to run the scripts. The scripts are actually run within our software. Unfortunately, I don't really think I can even run it from the command line, because we have a custom module that is imported, and the IDLE command line doesn't recognize it, or something. It gives me an error saying there is no module named vscript, because thats a part of our software...

Our software engineer is looking into it as well, but for a straight file open/write/close from standard python code, which works up to a point and then starts giving me errors, well, I was just hoping there'd be an easy solution that my python inexperience doesn't let me see... lol.

I'm coming a long way, since 6 months ago I didn't even know Python existed, but I still have a lot to learn.

Anyways, thanks for all the help everyone.
Jul 9 '07 #13
bartonc
6,596 Expert 4TB
Unfortunately, I'm not supposed to move to a different version of Python, as our software is written with 2.3 in mind. Our software engineer said we shouldn't use any other version at the moment, as he isn't sure what the effects would be.

As for IDLE, I'm only using it to edit the python code, not to run the scripts. The scripts are actually run within our software. Unfortunately, I don't really think I can even run it from the command line, because we have a custom module that is imported, and the IDLE command line doesn't recognize it, or something. It gives me an error saying there is no module named vscript, because thats a part of our software...

Our software engineer is looking into it as well, but for a straight file open/write/close from standard python code, which works up to a point and then starts giving me errors, well, I was just hoping there'd be an easy solution that my python inexperience doesn't let me see... lol.

I'm coming a long way, since 6 months ago I didn't even know Python existed, but I still have a lot to learn.

Anyways, thanks for all the help everyone.
Dang... I was really hoping for a good guess on that one. It leaves an empty feeling when a member signs off of a thread like this. I'll keep the thread open in case you come up with a solution that you would like to share.

regards,
Barton
Jul 9 '07 #14
Dang... I was really hoping for a good guess on that one. It leaves an empty feeling when a member signs off of a thread like this. I'll keep the thread open in case you come up with a solution that you would like to share.

regards,
Barton
Thanks Barton. As an update (and I am planning on it until I get it solved, or worked around), my software engineer had me move from 2.3 to 2.3.4, because apparently he found info on a bug about this that supposedly was fixed in 2.3.4, but not other releases.

I tried it, and it didn't work either.

Here's another question for you:

I have this file being accessed in about 180 different scripts (maybe only about 50 of them actually use this code, and then maybe not all of them at the same time)

Anyway, my question is this... I open the file with a write method only once:

Expand|Select|Wrap|Line Numbers
  1.     if getComponent().name == "D100":
  2.         AGVSeqLog = open("C:\\V-Sim\\cdata\\Sandbox\\Oshawa\\Documentation\\AGV_Seq.txt","w")
  3.         simtime = str(world.simTime.raw)
  4.         realtime = str(datetime.now())
  5.         AGVSeqLog.write("Log File Open" +" RealTime: "+ realtime +" SimTime: "+ simtime +"\n")
  6.         AGVSeqLog.flush()
  7.         AGVSeqLog.close()
  8.  
Then, in the scripts, I append to the file with ("a"), each time and in each script opening, appending, flushing and closing:

Expand|Select|Wrap|Line Numbers
  1.         AGVSeqLog = open("C:\\V-Sim\\cdata\\Sandbox\\Oshawa\\Documentation\\AGV_Seq.txt","a")
  2.         Loop = str(getComponent().name)
  3.         simtime = str(world.simTime.raw)
  4.         AGVNum = str(AGV.getParameter("O_VehNum").value)
  5.         AGVSeqLog.write(" " + Loop + " SimTime: "+ simtime + " AGV#: " + AGVNum + "\n")
  6.         AGVSeqLog.flush()
  7.         AGVSeqLog.close()
  8.  
I'm wondering if I might be able to eliminate the error, or maybe just increase efficiency, by leaving it open at the first, and then just appending to it at every other time without ever closing it.

Do you think that might help the problem? Would other problems be caused if I never close the file? Keep in mind that this file is accessed by multiple scripts.
Jul 9 '07 #15
bvdet
2,851 Expert Mod 2GB
Markalon,

You should not need to flush the file object, because close() forces buffered output data to be flushed. If you do not close the file, keep the flush().
Jul 9 '07 #16
Markalon,

You should not need to flush the file object, because close() forces buffered output data to be flushed. If you do not close the file, keep the flush().

UPDATE: Error resolved.

Not entirely sure how or why, but I relocated the offending code to a different area of the script. Seems to have solved the problem, but I have no idea what difference it made.

bvdet,

Thanks for the info. It makes sense. But if I have a single text file, where 180 different scripts are writing to it, do I need to open/close each write? Or can I just open it once, then write to it from 180 different scripts? Will it help increase efficiency? Is there a problem with writing to one open file from 180 different scripts?
Jul 9 '07 #17
bvdet
2,851 Expert Mod 2GB
UPDATE: Error resolved.

Not entirely sure how or why, but I relocated the offending code to a different area of the script. Seems to have solved the problem, but I have no idea what difference it made.

bvdet,

Thanks for the info. It makes sense. But if I have a single text file, where 180 different scripts are writing to it, do I need to open/close each write? Or can I just open it once, then write to it from 180 different scripts? Will it help increase efficiency? Is there a problem with writing to one open file from 180 different scripts?
I would suggest closing each file in every script. You could do testing with timeit to determine the most efficient way, but I doubt it would make much difference. Besides that, it's good practice for each script to tidy up by closing files.

The error you were receiving has been seen by others in the past in subthreads. It was supposedly fixed in 2.4. Google the error message to see the discussions.
Jul 9 '07 #18
bartonc
6,596 Expert 4TB
UPDATE: Error resolved.

Not entirely sure how or why, but I relocated the offending code to a different area of the script. Seems to have solved the problem, but I have no idea what difference it made.

bvdet,

Thanks for the info. It makes sense. But if I have a single text file, where 180 different scripts are writing to it, do I need to open/close each write? Or can I just open it once, then write to it from 180 different scripts? Will it help increase efficiency? Is there a problem with writing to one open file from 180 different scripts?
The technique that I use with a database connection would work well for you here:
Open the connection (or file, in your case) just once. Then pass a reference to that file object to the functions/objects/methods that need it. So it's
Expand|Select|Wrap|Line Numbers
  1. def Worker(fileToWorkOn, *args): # use the file object instead of a file name
  2.     # do stuff
  3.  
  4. f = file("filename", "w")
  5. Worker(f, "whatever")
  6. # Etc.
  7. f.close()
Jul 9 '07 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Uwe Mayer | last post: by
30 posts views Thread by Adam Baker | last post: by
3 posts views Thread by Paul Rudin | last post: by
reply views Thread by Aspersieman | last post: by
reply views Thread by Gabriel Genellina | last post: by
185 posts views Thread by jacob navia | last post: by

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.