I need to copy a file from a remote machine.
The code is something like this
import shutil
shutil.copyfile('//XXX.XXX.XXX.XXX/home/Shares Folder/file.txt','/home/file.txt')
the location of the file is a shared folder but everytime I run this it gives me this error
File "", line 1, in ?
File "usr/lib/python2.4/shutil.py", line 47, in copyfile
frsc = open (src,'rb')
IOError: [Errno 2] No such file or directory :
'//XXX.XXX.XXX.XXX/home/Shares Folder/file.txt'
Please take note that I'm running this script in my current machine and the the file that I want to copy is in the remote machine. I am not sure if this detail's relevant, I'm saying anyway.
Im 100% sure that the file is there so I was wondering why it's giving me such error message.
Im using CentOS and python 2.4.3
any ideas?
shutil doesn't support remote file copies. It's for local copies only.
If both of your machines are UNIX-based, you can try and employ some modules for a transport that you have available (SSH/SFTP, rsync, whanot)
You've also tagged with samba and shared-folders - if you're trying to copy via samba, you might want to look into pysmbc or a similar python samba library. http://pypi.python.org/pypi/pysmbc/ even has an example of opening and reading a file over samba - it's a short step to writing the contents out locally.
Related
data = pd.read_excel("ETH-USD")
I continually receive an error message informing me that the file cannot be found
this is despite the fact that
1: my directory has been changed within to Python to the same address as the folder where the excel file is stored
2: the file name is input exactly as displayed
Any ideas on why it is unable to find my file?
Is it possible that the excel file has an extension of .xlsx, but your file explorer is set to "hide file extensions"? Try:
data = pd.read_excel("ETH-USD.xlsx")
Or, see what's in the current directory by running:
import os
print(os.listdir())
A tip from the comments:
Windows tip too: hold Shift, right click the excel file and copy as path, then you can see its path (if you don't enable viewing file extensions in the file browser). –
creanion
Often when running python scripts from compilers the "working directory", or where you are running the script from doesn't match the location of your script, hence why I find it much more reliable to use this instead:
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
data = pd.read_excel(os.path.join(BASE_DIR,"ETH-USD")
To add, while I do not use Jupyter, in VSCode which I use, the working directory (which is where python looks for if you put a path in read_excel() but its not a full path) is often the current directory opened in there, so I expect a similar issue to be the reason for your issue.
I am trying to read an excel file from a NAS using Jupyter Notebook (macOS, Python 3, SynologyDS218+).
Script worked absolutely fine when the file was stored locally, but I cannot determine the correct code or file path adjustment to access the file once moved to the NAS.
I am logged into the NAS and from Mac Finder the file path is:
Server: smb://NAS/home/folder/file.xlsx
I have reviewed...
How to read an excel file directly from a Server with Python
Python - how to read path file/folder from server
https://apple.stackexchange.com/questions/337472/how-to-access-files-on-a-networked-smb-server-with-python-on-macos
... and tried numerous code variations as a result, but with no success.
I am using:
pd.read_excel(“//NAS/home/folder/file.xlsx”, sheet_name=‘total’, header=84, index_col=0, usecols=‘B,AL,DC’, skiprows=0, parse_dates=True).dropna()
But regardless of the code/file path variation, the same error is returned:
FileNotFoundError [Errno 2] No such file or directory: //NAS/home/folder/file.xlsx
I finally located the correct code / file path adjustment necessary to read the file. See https://apple.stackexchange.com/questions/349102/accessing-a-nas-device-from-python-code-on-mac-os
Although the drive was mapped and Mac Finder indicated a file path of "smb://NAS/home/folder/file.xlsx", using Control+Command+C instead, to copy the file path from Finder to the clipboard returned "/Volumes/home/folder/file.xlsx". Using this file path located the file and allowed it to be read.
I have a python script that reads and writes to files that are located relative to it, in directories above and beside it. When I run my script via Cygwin using
python script.py
The program works perfectly. However, when I run it by navigating through the windows GUI to my file and double clicking, I get a blank cmd prompt and then my program runs fine until I reach the point where I need to access the other files, at which point it fails and gives me this message in the cmd prompt that opens itself:
../FFPRM.TXT
../2025510296/FFPRM_000.TXT
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Users\rbanks\Desktop\TSAC\EXECUTABLE\T-SAC_GUI.py", line 705, in run_exe
invalid_entry, output_text = self.apply()
File "C:\Users\rbanks\Desktop\TSAC\EXECUTABLE\T-SAC_GUI.py", line 694, in apply
p = subprocess.Popen(['cp', output_file_path, output_file_path_id])
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\Users\rbanks\AppData\Local\Programs\Python\Python35-32\lib\subprocess.py", line 1220, in _execute_child startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I am deploying this script as well as the directory structure as a zip for users to be able to unzip and use anywhere on their PC, so it is important for me to be able to run it with a simple double click and my relative file paths.
My first thought was the cmd prompt that was opening and executing my script was in a different environment, but when I run:
cd
pause
in a .cmd script, I get:
C:\Users\rbanks\Desktop\TSAC\EXECUTABLE>pause
Which is the correct location.
I am not having any luck with Google, I assume because I can't seem to construct a sufficient search query. Could someone point me in the right direction please?
[edit] the other answer is correct(at least i suspect) but i will leave this here in the hopes that it helps the op in the future with path problems ... and doing something like this is just generally good practice
use
BASEPATH = os.path.abspath(os.path.dirname(__file__))
at the top of your script
the later
txt_file = os.path.join(BASEPATH,"my_file.txt")
or even
txt_file = os.path.join(BASEPATH,"..","my_file.txt")
this gives you the benefit of being able to do things like
if not os.path.exists(txt_file):
print "Cannot find file: %r"%txt_file
which will likely give you a better idea about what your problem might actually be (if its simply path related at least)
The problem is not the current directory. It is correct when double clicking on the icon
The problem is: Cygwin commands are not in the windows path
You are using python, so don't run simple copy commands like this, which make your script non-portable and subject to variations, requiring installation of cygwin, etc...
p = subprocess.Popen(['cp', output_file_path, output_file_path_id])
can be replaced by
import shutil
shutil.copyfile(output_file_path, output_file_path_id)
shutil module complements os module for file manipulation functions that aren't native in the operating system.
now you have a 100% pythonic solution, native, which will throw exceptions if cannot read/write files, so fully integrated in the rest of your program.
Before running an external command from Python make sure that no python way exists. There are so many useful modules out there.
Other examples of how to avoid running basic commands from python (of course if you need to run a C compilation it's different!):
zipfile package: much better than running zip.exe
gzip package: can open gzipped files natively from python
os.listdir() instead of parsing the output of cmd /c dir /B
os.remove() instead of calling rm or del
etc... python rules!
Coming from here:
Basic http file downloading and saving to disk in python?
Is there any possibility to save the file in any folder? I tried this but i get error:
IOError: [Errno 2] No such file or directory:
import urllib
testfile=urllib.URLopener()
testfile.retrieve("http://randomsite.com/file.gz","/myfolder/file.gz")
Any possibility to do it?
You're most likely getting that error because /myfolder doesn't exist. Try creating it first
import os
import os.path
import urllib
destination = "/path/to/folder"
if os.path.exists(destination) is False:
os.mkdirs(destination)
# You can also use the convenience method urlretrieve if you're using urllib anyway
urllib.urlretrieve("http://randomsite.com/file.gz", os.path.join(destination, "file.gz"))
The directory /myfolder/file.gz doesn't available in your server or pc. Make a real file path which exists in your pc or server. For example:
./file.gz
file.gz
This will save the file from where you are running your script. In other words, in the same location to your python script.
I am trying to run a python script which uses a binary file (xFiles.bin.addr_patched) created by a postlinker. However, I am getting this error.
File "abc.py", line 74, in ParseCmd
shutil.copy(gOptions.inputX, gWorkingXFile)
File "/usr/lib/python2.6/shutil.py", line 89, in copy
copymode(src, dst)
File "/usr/lib/python2.6/shutil.py", line 66, in copymode
os.chmod(dst, mode)
OSError: [Errno 1] Operation not permitted: 'myPath/xFiles.bin.addr_patched'
When I checked the permissions of this xFiles.bin, by ls-l, it shows that
-rwxrwxrwx 1 nobody nogroup
I presume the error is because this file was created by some other application, the python script I am running does not have access to it. Since I am beginner wrt ubuntu, I don't really know how to fix it. Any suggestions on how to fix this?
SOLVED:
As one of the answers Suggested : chown username:groupname file name fixes this issue
You could try (from the command line, but I'm sure there's a syntax in python):
sudo chown your_username:your_groupname filename
Note: The group is usually just your username.
I feel like there's something wrong with those permissions though. Read Write Execute for everyone seems to be off. How was this file created? How did it get to be created by the user nobody?
Python code to change the permission:
from getpwnam import pwd
from getgrnam import grp
import os
uid = getpwnam("YOUR_USERNAME")[2]
gid = grp.getgrnam("YOUR_GROUPNAME")[2]
os.chown("myPath/xFiles.bin.addr_patched", uid, gid)
Run the script with sudo and you're done.
I had this problem when running a python script on my mac (10.14 Mojave) trying to access /Users/xxx/Pictures/Photos Library.photoslibrary.
The full solution can be found in http://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/
Summary:
Go to System Preferences > Security & Privacy > Privacy > Full Disk Access and add your IDE or python interpreter to the list.
My guess is that you should be looking at the permissions for myPath folder instead. Seems like you can't write to it, hence the problem. Try ls -l myPath/.. and see the permissions for myPath. If that's the problem, change the permissions on the folder with chmod.
P.S. Also, see Google top result on Linux file permissions.