I am hosting a text file on OpenShift inside the cron/minutely directory. The server is trying to run the text file every minute, only I don't want it to: all I want the text file to do is stay on the server so I can save useful information inside it.
How do I tell the server that I want it to ignore the text inside the file instead of running it as code? Is there a way to do it by adding a specific "shebang" line at the beginning? Is it even possible to keep the text file in the cron/minutely directory without it being executed?
Thank you.
You can add this as the first line of the text file:
#!/usr/bin/true
That's a sort of fake shebang line. It means that if the file is executed, the "interpreter" to "run" the file is the true program, which is a trivial program which does nothing but return the status code for success (0).
Therefore, a true shebang will make any file "successfully" execute, but do nothing.
Please don't take this to mean that leaving data files in a directory meant for executables is a good idea; it's not. This is just a hack of last resort.
Related
I have a python file that isn't working.
I would like to run this script on it:
with open('beak', 'rb+') as f:
content = f.read()
f.seek(0)
f.write(content.replace(b'\r', b''))
f.truncate()
Source
I don't know how to make multiple lines on command line and I am not really sure how to execute my code. Do I just put my file name in place of 'beak' and do I just cd to the folder my file is in before I execute this script?
You can type that into the Python command line. Type the first line and return, it will recognize that you're in the middle of a with clause and allow you to type the remaining lines one at a time (be sure to get the indentation right). After the last line, return twice and it will execute.
This script assumes that you are going to read a file named "beak". You need to run this script from the same directory where "beak" is. ("beak" should really have an extension, like ".txt", depending on what kind of file it is).
Doing long scripts from the command line like this is not the best way -- it's better to put this code in a file ("reader.py", for example -- and put reader.py in the same directory as "beak"). Then you can simply execute by typing "python reader.py".
I have a Python script that runs properly on my laptop, but when running on my raspberry pi, the following code does not seem to be working properly. Specifically, "TextFile.txt" is not being updated and/or saved.
openfile = open('/PATH/TextFile.txt','w')
for line in lines:
if line.startswith(start):
openfile.write(keep+'\n')
print ("test 1")
else:
openfile.write(line)
print ("test 2")
openfile.close()
I am seeing "test 1" and "test 2" in my output, so I know that the code is being reached, paths are correct, etc
It may be due to a permissions problem. I am running the script from the terminal by using:
usr/bin/python PATH/script.py
Python is owned by "root" and script.py is owned by "Michael".
My first guess:
Does the file exist? If it does not exist then you cannot write to it. Try this to create the file if it does not exist: file = open('myfile.dat', 'w+')
Additionally manually opening and closing file handles is bad practice in python. The with statement handles the opening and closing of the resource automatically for you:
with open("myfile.dat", "w+") as f:
#doyourcalculations with the file object here
for line in f:
print line
All, thank you for your input. I was able to figure out that it was writing to the new file, but it was overwriting with the same text. The reason was because ".startswith" was returning false when I expected true. The misconception was due to the difference between how Windows and Unix treat new line characters (/n /r).
Since your code is running, there should be a file somewhere.
You call "PATH/script.py", but there is "/PATH/TextFile.txt" in your program. Is the slash before PATH a mistake? Have you checked the path in your program is really where you are looking for the output file?
I have a python script that makes a .xml file. I am running this script on my server. And it runs fine and at the end of the script I put the generated file on my server like this.
output_path = "/path_to_my_loication/"
ofname = "name_of_file.xml"
output_file = output_path + ofname
open(output_file, "w").write(str(BeautifulSoup(get_xml(menu_url_list[0][1]))))
and is just part of making the file, pretty sure its irrelevant to this. So run the script on my server and it outputs my file, and I can see it, but the url is not correct instead of it being.
myserver/path_to_my_loication/name_of_file.xml
it is
myserver/path_to_my_loication/%0d%0dname_of_file.xml
This is being added %0d%0d before the file name I am not sure how to fix this, and when I print my file name just before I write the file, the characters are not there? How can I get rid of this/ why does this happen.
When I just run the script on my laptop the file name does not contain theses characters. Is it a server side issues that I should contact my serve provider about?
Thanks for the help.
Okay AIG cause when I printed for my server I saw this, I though enter was just some weird thing
How can I get rid of the enters?
Here is full code
https://gist.github.com/spennyf/de3349252695cecd4e6c
I don't exactly know how to phrase this question, but I have tried my best.
Let's say I have an application and I have set it to be the default to run when the .example file extention has been double clicked within File Explorer. Now, how do I get this application not to just launch, but instead to react as if it has been asked to open the file. Now I know there is the:
file = open ("C:\\ExampleFolder\\ExampleFile.example)
file = file.read()
method of reading a file but how would I get the program to run this script when the program is launched by the opening of a file and in which way would I get the program to know the location of the opened file? I know that there are questions about setting a python app to the default but I have found nothing, if I am asking the question right, on completing the above on stackoverflow or a Google search. Or I am just looking at this wrong or asking the question wrong?
I'm not sure this might be what you are looking for. You can check whether, double clicking on a file with extension .example which you have linked to your program, means that an argument is passed to your program: the file name being opened. You can check whether this is the case by:
import sys
print sys.argv
#if that is meaningless, try printing every single argument passed to the script
#I can't recall whether len(sys.argv) is also = sys.argc!
for x in xrange(len(sys.argv)):
print sys.argv[x]
I hope this is of some help!
I am trying to port a Sublime Text build system to a plugin.
The build system would receive the current file and go through it with this code:
for line in fileinput.input(inplace=1):
sys.stdout.write(makeReplacements(line))
Now, in the plugin syntax I go the fact that the way to get my current file's content is:
input = self.view.substr(
sublime.Region(0, self.view.size())
)
But now I'm not sure about what I should do about the next operation.
for line in input(inplace=1):
How could I make replacements in the file on-the-fly and then save it?
I don't think the Sublime Text plugin API can save the buffer, but you could use the file_name() method in the sublime.View class and work with the file directly.
As noted by #MattDMo, the file can be saved using view.run_command('save').
It may be easier to use the file name if your old build file worked with that.
As stated by #RazerM, the first argument has to be the file path. So for my example, this will work.
for line in fileinput.input(self.view.file_name(), inplace=1):
sys.stdout.write(self.makeReplacements(line))