Download file with urlretrieve() to subfolder - python

Is it possible to use urlretrieve() in order to download something into a subfolder without expressing it in an absolute but relative manner?
For example:
urllib.request.urlretrieve(url, '/downloads/2017/foo.txt')
Everytime I add a path to the filename python throws following error:
File "C:\Program Files\Python36\lib\urllib\request.py", line 258, in urlretrieve
tfp = open(filename, 'wb')
FileNotFoundError: [Errno 2] No such file or directory: '/downloads/2017/foo.txt'
But when I use this code:
urllib.request.urlretrieve(url, 'foo.txt')
it happily downloads the file.
I think I am missing something basic here, but after searching the interwebs for quite a while I haven't found an answer yet. Does anyone know how relative filepaths should be expressed in the urlretrieve() method?

When using urllib.request.urlretrieve(), you need to make sure that, if you want to save the file somewhere else than the program's location, the folders you are referencing already exist. Alternatively you can use the following code to generate the folders you need if they do not exist:
for i in directories:
if not os.path.isdir(i):
os.makedirs(i)
Check for spelling mistakes if you still get a FileNotFoundError.

Related

Python: FileNotFoundError, from glob output, full path to file is correct

I hate to be the person to post another FileNotFoundError question, but most of them that I see are about not giving the full path to the file, that is not my problem here.
I have a number of log files in folders in ../../Data/. I create a glob of those files using
DataFiles = glob('../../Data/2021*/*.log')
I want to open each of the files in that glob, so I use
for i, file in enumerate(DataFiles):
with open(file, "r") as f:
...
etc. 99% of these open correctly and the rest of the code runs. For some reason, a few will not. I get an error like
FileNotFoundError: [Errno 2] No such file or directory: '../../Data\\20210629_081706\\20210629_081706_data.log'
The file definitely exists, that's why it was found by glob. The full path is used. And,
from pathlib import Path
Path('../../Data\\20210629_081706\\20210629_081706_data.log')
returns
WindowsPath('../../Data/20210629_081706/20210629_081706_data.log')
So does anyone know what might be happening here?
A bit late, but I had the same error when using glob in a network folder with way to many levels.
There was a particular folder where some of the files caused that error, and those files couldn't even be opened by the explorer itself:
In my case this was caused by the path being over 260 characters in length.
You can try something like suggested here to allow handling files with larger paths, or just make sure the path is short enough for the explorer to handle it.

pandas cannot read csv in same directory

i am having this issue since like 2 months, it didnt bother me at first but now that im trying to import a file with pd or even a normal txt file with open() it gives me this Exception:
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 642, in get_handle
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'marketing.xlsx'
if i use the full path it just runs normally.
people would say "just use full path then" but this is a really bad solution when it comes to using this program on multiple devices with different paths or stuff like that
so i hope you have any solutions.
here is the code:
import pandas as pd
df = pd.read_csv('marketing.xlsx')
image:
vscode
folder
edit:
it has none to do with the code itself but more like windows or my pc
FileNotFoundError means that the file path you've given to pandas point to an non existing file.
In your case this can be caused by:
you haven't put your file in the current working directory of your script
you have a typo in your file name
In both case, I would print the file path, and check the location using a file browser, you will find your mistake:
print(os.path.join(os.getcwd(), 'marketing.xlsx'))
i see a spaces in the file name there. I tried on mac, and it works very well.
['main.py', 'marketing .xlsx', 'requirements.txt']

Not finding any solution to [Errno 2]

I'm trying to use a program to read from a file with pi-digits. The program and the text file with the pi-digits are in the same directory, but i still get the error message :
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents.rstrip())
Traceback (most recent call last):
File "C:\Python\Python_Work\python_crash_course\files_and_exceptions\file_reader.py", line 1, in <module>
with open('pi_digits.txt') as file_object:
FileNotFoundError: [Errno 2] No such file or directory: 'pi_digits.txt'
I have looked for a solution but haven't found any.
I found a piece of code which supposedly shows me what the working directory is. I get an output that shows a directory that is 2 steps above the directory i have my programs and text file inside.
import os
cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))
So when i put the pi text document in the directory that the output is showing (>python_work), the program is working. When it does not work is when the text file is in ">files_and_exceptions" which is the same file the program itself is inside. My directory looks like this when it is not working:
>python_work
>python_crash_course
>files_and_exceptions
file_reader.py
pi_digits.txt
show_working_directory.py
And like this when it is working:
>python_work
pi_digits.txt
>python_crash_course
>files_and_exceptions
file_reader.py
show_working_directory.py
I'm new to python and really appreciate any help.
Thanks!
Relative path (one not starting with a leading /) is relative to some directory. In this case (and generally*), it's relative to the current working directory of the process.
In your case, given the information you've provided, for it would be "python_crash_course/files_and_exceptions/pi_digits.txt" in the first case as you appear to be running the script from python_work directory.
If you know the file to be in the same directory as the script itself, you could also say:
import os.path
...
os.path.join(os.path.dirname(__file__), "pi_digits.txt")
instead of "pi_digits.txt". Or the same using pathlib:
from pathlib import Path
...
Path(__file__).with_name("pi_digits.txt")
Actually unless you have a point to anchor to like the script itself, using relative filename / path (using absolute paths brings its own problems too) in the code directly is rather fragile and in that case getting it as a parameter of a function (and ultimately argument of CLI or script call in general) or alternatively reading it from standard input would be more robust.
* I would not make that an absolute statement, because there are situations and functions that can explicitly provide different anchor point (e.g. os.path.relpath or openat(2); or as another example a symlink target)

Python says it can't find the file I am trying to rename even though it can read the file name

Im tryin to append a short string to every '.png' file. But when I run it, it says cannot find the file. But I know it's there and I can see it in the folder.
Is there anything I need to do?
Here is my script:
import os
for file in os.listdir("./pics"):
if file.endswith(".png"):
newFileName = "{0}_{2}{1}".format(*os.path.splitext(file) + ("z4",))
os.rename(file, newFileName)
Here is the error message I get...02.png is the first file in the folder:
fileNotFoundError: [WinError 2] The system cannot find the file
specified: '02.png' -> '02_z4.png'
It's odd though because it gets the filename, in this case, 02.png. So if it can read the file name, why can't it find it?
Thanks!
I thought my comment might be enough, but for clarity I'll provide a short answer.
02.png doesn't exist relative to your working directory. You need to specify the path to the file for os.rename so you need to include the directory.
import os
for file in os.listdir("./pics"):
if file.endswith(".png"):
newFileName = "/pics/{0}_{2}{1}".format(*os.path.splitext(file) + ("z4",)) # Notice the ./pics
os.rename(os.path.join('pics', file), newFileName)
The name returned from os.listdir() gives the filename, not the full path. So you need to rename pics/02.png to pics/02_zf.png. Right now you don't include the directory name.

Errno 2 using python shutil.py No such file or directory for file destination

I am using the shutil python module to copy files and directories on a linux redhat machine.
I wrote the following method, which takes in 2 params: src (the path of the file or dir that is being collected) and destination (the desired new path of where the collected log/dir is being pasted to).
def copy(src, destination):
if(os.path.exists(src)):
if(os.path.isdir(src)):
if(os.path.exists(destination)):
shutil.copytree(src, destination+getTimeStamp())
else:
shutil.copytree(src, destination)
else:
shutil.copy(src, destination)
else:
print src+" not found"
I have been using this method just fine, but I recently ran into an error when running this code:
copy("/home/midgar/logs/logger.xml", currentPath+"/testrun/logs/logger.xml")
The error: IOError: [Errno 2] No such file or directory: 'collectedLogs/testrun/logs/logger.xml'
I would understand what this error implies if the file or directory that it is looking for is the src, but this is the destination that is causing the error. I found out that this line of code that throws the error goes to the line: "shutil.copy(src, destination)" in my copy method.
So far, my copy method just overwrites existing files, and if there is an existing directory, it creates a new one with a time stamp. In this case, the destination file does not already exist anyways. So, what can be the issue? Why am I getting this error with the DESTINATION path (when I would typically expect to see this kind of error with the SRC path).
Could it possibly be because this is an .xml file?
When I get this error it usually means that one of the folders doesn't exist.
I wrote a simple script to to test this out. In the script below the backup folder does exist but the today folder does not. When I run the script I get the same error as you.
IOError: [Errno 2] No such file or directory: 'backup/today/my_file.txt'
import shutil
shutil.copy("my_file.txt", "backup/today/my_file.txt")
If all of your folders do exist I would check to make sure the permissions on them have not changed.
By default, shutil.copytree() follows (resolves) symbolic links. If the symlink is broken, it raises a No such file or directory exception. One workaround is to specify that symlinks should be copied unresolved by passing symlinks=True.
I also recently came across this error. In my case, the file wouldn't be created because the filename and directory structure exceeded the max 260 characters. Solution: choose a shorter filename (or full file path name).

Categories