I am trying to convert Fabric 1 to Fabric 2 script and I have the following statement to get the remote file:
c.get(os.path.join(working_dir, "dist/*.tar.gz"), "dist"))
It used to work just fine, copy a file from remote computer to a local directory called dist. However, with Fabric2 it fails with
"IsADirectoryError: [Errno 21] Is a directory: '/home/rapolas/projects/dist'"
Sure enough, it is a directory, but that is exactly what I want, to put that remote file (there is only one, but I don't have its name) into local directory. According to docs:
For example, if the local path is a directory, the remote path’s base filename will be added onto it (so get('foo/bar/file.txt', '/tmp/') would result in creation or overwriting of /tmp/file.txt).
but for some reason, it doesn't work. Am I doing something wrong?
Related
I have the following code to run a program located at
/Users/*/Downloads/Mood Script Static and another located a folder deeper at /Users/*/Downloads/Mood Script Static Edit/LowMood_Static. Now I am trying to invoke a subprocess controller that will run two python scripts, like this:
subprocess.call(['python3', 'SubjectSFbalance.py'])
print("\n")
time.sleep(0.5)
subprocess.call(['python3', 'LowMood_Static/LowMoodZScoring.py'])
However, when running the 2nd process, it correctly finds the .py script, but pandas cannot read the .csv files that are given in the LowMoodZScoring.py file, which is this:
Normalized_Subj = pd.read_csv('../SF_Output_Files/finished_sf_balance.csv')
giving the following error:
FileNotFoundError: [Errno 2] No such file or directory: '../SF_Output_Files/finished_sf_balance.csv'
However, when I run LowMoodZScoring.py without using the subprocess, I get no such error. What in subprocess is causing this error?
At first glance, since those CSV paths are relative, it looks like you need to add cwd="LowMood_Static" to the parameters of your subprocess call, so that it will run with that directory as the working directory.
To avoid this in a more comprehensive/future-proof way, it would be even better to have your LowMoodZScoring.py script load the CSV files relative to the directory of that script (i.e. os.path.dirname(__file__)), instead of relative to the current working directory.
I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)
I have myfile.py on my local machine.
I want to do something like:
from fabric.api import env, run
env.host_string = 'whatever.com'
def run_script():
run('python myfile.py')
but of course, this returns can't open file 'myfile.py': [Errno 2] No such file or directory How can I run this file remotely? Do I have to put it onto whatever.com?
You can first push your myfile.py to the remote machine using fabric.operations.put and then run the script like you've attempted to.
But make sure the path to your script is either an absolute path or relative to the current directory from which the remote commands are being executed, this can be found out using cwd you can also manually cd into the remote folder using fabric.context_managers.cd
I am trying to print the directory size using python fabric. I used the below code
def getfilesize():
with settings(user='hduser',password='cisco'):
path='/app/hadoop/tmp/myoutput/'
os.path.getsize(path)
but it throws me an error " no such file or directory"
But I can see this directory
hduser#dn1:~$ cd /app/hadoop/tmp/myoutput/
hduser#dn1:/app/hadoop/tmp/myoutput$ ls
taskTracker tt_log_tmp ttprivate userlogs
Am i doing any sytax error here ?
This isn't a syntax error, rather the python code is still being executed on your machine and not the remote machine. So calling os.path.getsize is checking the path size on your local machine (where it does not exist).
Instead you need to use fabric to execute shell commands on the remote server and catch their output. There are fabric modules that wrap common use cases, like the files module, so you don't have to work in terms of bash commands. Unfortunately I don't know of any that will give you a recursive directory size. Fortunately, getting the size of a directory is just a oneliner so we can do something like:
def getfilesize():
with settings(user='hduser', password='cisco'):
output = run('du -s "/app/hadoop/tmp/myoutput/"')
# output is the commands stdout as a (potentially multiline) string
# for `du` it will look like: "183488582 /app/hadoop/tmp/mypoutput"
size_in_bytes = int(output.split()[0])
I'm running OSX Mavericks but this problem has been going on since I had Snow Leopard.
When writing a script in any language, eg: Python. When I try to open a file the short
form doesn't work.
file = open('donkey.jpg')
And I get this error:
IOError: [Errno 2] No such file or directory: 'donkey.jpg'
Instead, I always have to specify the full path.
file = open('/Users/myName/Desktop/donkey.jpg')
Any ideas on why this could be happening and how to correct it?
If you specify donkey.png, it means donkey.jpg file in the current working directory. (relative path)
Make sure you're running the script in the same directory where donkey.jpg exists.
If you want specify the image file path relative to the script file instead of current working directory use following:
import os
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'donkey.jpg')
NOTE You can use __file__ only in script file. (not in interactive mode)
Your open call does not have a mode parameter. In which case, it defaults to opening the file in read mode.
Unless the file you are opening (to read) resides in the current working directory, it is completely expected that the python script throws a IOError.