Creating a text file using a python script that runs in background on Raspberry Pi
-
I'm having a problem about the topic in the title. I created a python script on the directory /home/pi/ and it starts running on the background when the Raspberry Pi is booted. It's duty is this: When I push a button that is connected to one of the GPIO's, it will create a folder in it's own directory, then create a text file called 'fileName.txt' at the directory /home/pi/; and write the name of the folder it just created, in this text file. Everything goes fine until the 'create a text file' part. I boot the Raspberry Pi, then I push the button. The script creates the folder that I want, but after that, it doesn't create the text file. Since it runs in background, I can't see the error on the terminal that may explain the problem. After that, I tried starting the script manually to see the error message; however, this time it worked perfectly well. It created the text file and wrote the name of the folder in it. This is the simple code that I use to create the file: text_file = open("folderName.txt", "w") text_file.write("%s" %folderName) text_file.close() Anyone knows how to solve it?
-
Answer:
It is probably creating the file in the wrong folder. Try specifying the absolute path where you want to create the file, or discover it inside the script, like the example below: import os cwd = os.path.dirname(os.path.abspath(__file__)) text_file = open(os.path.join(cwd, "folderName", "textFile.txt"), "w") Also, to test a script that is running in background, you can write debug messages to a log file in /var/log (might need root permission) or /tmp, either using the Logging module with a FileHandler, or using the open built-in function.
dnzzcn at Stack Overflow Visit the source
Related Q & A:
- how to parse a xml file using jquery and phonegap?Best solution by Stack Overflow
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- Can you send a text message to a mobile phone if so how?Best solution by Yahoo! Answers
- How do you send a text message to a phone from yahoo mail?Best solution by eHow old
- How do I open a .doc file using yahoo mail?Best solution by answers.yahoo.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.