User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Am I being thick? File write problem.

Sat May 10, 2014 1:11 pm

I'm slightly embarrassed to post this but...

Code: Select all

f = open("test.txt", "w")
f.write("testing\n")
f.close()
The file is created but the string isn't being written to it. Why?

Dave.
Apple say... Monkey do !!

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Am I being thick? File write problem.

Sat May 10, 2014 1:22 pm

Works OK for me.

Code: Select all

hard /ram $ ./x.py
hard /ram $ cat test.txt
testing
hard /ram $ cat x.py
#!/usr/bin/env python

f = open("test.txt", "w")
f.write("testing\n")
f.close()
hard /ram $ 

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Am I being thick? File write problem.

Sat May 10, 2014 1:39 pm

Okay, it works from the command line but not from within IDLE.
The proper script only needs to be run once to grab some info off t'internet and save it so I can live with the command line, but I'm still confused as to why it won't work from IDLE.

Dave.
Apple say... Monkey do !!

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Am I being thick? File write problem.

Sat May 10, 2014 1:42 pm

davef21370 wrote:Okay, it works from the command line but not from within IDLE.
The proper script only needs to be run once to grab some info off t'internet and save it so I can live with the command line, but I'm still confused as to why it won't work from IDLE.

Dave.
Perhaps IDLE is creating the file in a different directory?

You could check that by using the full path to the file (e.g. /home/dave/test.txt) rather than the relative path you are using.

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Am I being thick? File write problem.

Sat May 10, 2014 1:54 pm

joan wrote:Perhaps IDLE is creating the file in a different directory?
I delete the file after each unsuccessful attempt and it reappears in the current directory when I retry the script. I have it running from the command line as we speak and all's well but no doubt this will rear it's ugly head again.
Thanks anyway.

Dave.
Apple say... Monkey do !!

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Am I being thick? File write problem.

Sat May 10, 2014 2:36 pm

davef21370 wrote:
joan wrote:Perhaps IDLE is creating the file in a different directory?
I delete the file after each unsuccessful attempt and it reappears in the current directory when I retry the script. I have it running from the command line as we speak and all's well but no doubt this will rear it's ugly head again.
Thanks anyway.

Dave.
The only other things I can think of is a permissions problem. But if iDLE can create the file it should be able to write to it. Perhaps try a file flush in IDLE?

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Am I being thick? File write problem.

Sat May 10, 2014 4:12 pm

Does it make any difference if you use the 'with' context formatting, i.e.

Code: Select all

with open("test.txt", "w") as f:
    f.write("testing\n")
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Am I being thick? File write problem.

Sat May 10, 2014 11:47 pm

I've come across other things (perfectly good python) that don't work with IDLE. Is there any good reason why you would persist in using it?
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Am I being thick? File write problem.

Sun May 11, 2014 12:51 pm

elParaguayo wrote:Does it make any difference if you use the 'with' context formatting, i.e.

Code: Select all

with open("test.txt", "w") as f:
    f.write("testing\n")
Not in a position to try that at the moment as my Pi is tied up trying to save the feelings of cattle (may post more details on that depending on the outcome) but will try as soon as and update accordingly.
paddyg wrote:I've come across other things (perfectly good python) that don't work with IDLE. Is there any good reason why you would persist in using it?
I do most of my coding on the Pi and find IDLE quick and easy. The bit of C I write is done using Geany which I find sluggish and awkward.
I can understand IDLE having bugs like any other program but something as simple as writing to a file??

I'll bottom it somehow.
Thanks all.
Dave.
Apple say... Monkey do !!

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Am I being thick? File write problem.

Sun May 11, 2014 2:35 pm

'save' as in record the feelings or as in avoid hurting the feelings. Please tell us more, whatever the results.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Am I being thick? File write problem.

Sun May 11, 2014 2:56 pm

paddyg wrote:'save' as in record the feelings or as in avoid hurting the feelings. Please tell us more, whatever the results.
Kind of avoiding animals too much suffering.
The issue is to do with Halal meat and the way these animals are slaughtered.
The script I had problems with was to grab all UK politicians contact details and write them to a file (not that straightforward but sorted out).
The Pi is now tied up sending all of them a personal email (500+) at 6 minute intervals to avoid spamming.
I've already had lots of automated replies saying they'll respond when they get chance to read the email themselves but not holding my breath.

We can only hope.
Dave.
Apple say... Monkey do !!

User avatar
paddyg
Posts: 2555
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: Am I being thick? File write problem.

Sun May 11, 2014 6:02 pm

Good luck with that (getting any kind of action from our politicians).

The only animal slaughter I have observed in detail was from a bus going from Turpan to Kashgar - so it happened to be halal. If I was a goat, I can't imagine a happier death. One second being led by my devoted goatherd past the old men and children playing happily in the village square, the next lying in large pool of blood in the dust. No cattle trucks, concrete pens, sinister men in blood stained overalls or being bludgeoned over the back of the head!

Maybe there's scope for the raspberry pi to provide low cost, virtual, pastoral environments for these beast about to meet their doom.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”