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.
Related
I am following a tutorial (Beautiful Soup) and I can't seem to get open with to create a .txt file as I am getting this error:
with open(f'posts/{index}.txt', 'w+') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'posts/1.txt'. I tried looking up different solutions to my problem currently running python 3.9. Is there another step I need to correct this issue?
The interpreter isn't finding the directory "posts" in your current working directory. If it exists, you are not in its parent directory. Try using the full path, e.g. rf'C:\users\myuser\posts\{index}.txt' or rf'~/posts/{index}.txt' on linux.
Or, if it doesnt't exist, add these lines:
import os
os.mkdir('posts')
You can find your current working directory this way:
import os
os.path.abspath('.')
I am trying to automate data download from github. Assume the file in github is in a zip folder. Is there a way to write a python code to download the entire zip folder?
I have used the request method but it does not work. The code can run without error but the file is clearly smaller than the original file and I cannot open it by double click with error message file is invalide
import requests
URL = 'https://github.com/XYZ/file.zip'
r = requests.get(url)
# open method to open a file on your system and write the contents
with open("file.zip", "wb") as code:
code.write(r.content)
one way to workaround is to run a shell command in Python to clone the file then unzip on local using Python code
from subprocess import check_output
cmd = '!git clone https://github.com/XYI/file.git C:/Users/UserName/Desktop/test'
check_output(cmd, shell=True).decode()
I'm working with large netcdf files (3D gridded atmospheric data) that live on a server in my department. And I'm having trouble importing them. I try something like this:
from netCDF4 import Dataset
filename = 'nfs://path-to-file/file.nc'
file = Dataset(filename)
This has worked perfectly fine for files stored within my root directory, but because the server exists behind/outside of my root, I get an error when I try to import the files into a jupyterlab notebook:
FileNotFoundError: [Errno 2] No such file or directory: b'nfs://path-to-file/file.nc'
But I copied the absolute path from finder (Mac OS) so I'm sure that it's there.
I've tried telling python to look in the server using
import sys
sys.path.insert(0, 'nfs://server-name/path-to-file/')
But that hasn't worked and gives the same error as above. Will I need to change something else using python's sys or os packages? The files are large so I don't want to copy them to my local machine.
I have uploaded a package to pypi and github. I have then installed the package and tried to use it. It contains a python script which need to read from a file. I have placed both in the same directory.
pip install pycricket
from pycricket import cricket
c = cricket.Cricket()
c.query()
Query() function involves reading from a file. When I see the 'pycricket' package in library, both script as well as file are in same folder.
query():
with open('matches.csv', 'r') as f:
#code
I don't know why I get the error.
You can inspect the current working directory with:
>>> import os
>>> os.getcwd()
If your data is in a different directory (unclear from the question, but likely given the error message), then change to the directory where the data is stores:
>>> os.chdir(path_to_data_directory)
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.