[issue43219] shutil.copy raises IsADirectoryError when the directory does not actually exist

2021-02-13 Thread Jeremy Pinto


Jeremy Pinto  added the comment:

In fact, the issue seems to be coming from open() itself when opening a 
non-existent directory in write mode:

[nav] In [1]: import os
 ...: nonexixstent_dir = 'not_a_dir/'
 ...: assert not os.path.exists(nonexixstent_dir)
 ...: with open(nonexixstent_dir, 'wb') as fdst:
 ...: pass
---
IsADirectoryError Traceback (most recent call last)
 in 
  2 dir_path = 'not_a_dir/'
  3 assert not os.path.exists(nonexixstent_dir)
> 4 with open(nonexixstent_dir, 'wb') as fdst:
  5 pass

IsADirectoryError: [Errno 21] Is a directory: 'not_a_dir/'

--

___
Python tracker 
<https://bugs.python.org/issue43219>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43219] shutil.copy raises IsADirectoryError when the directory does not actually exist

2021-02-13 Thread Jeremy Pinto


New submission from Jeremy Pinto :

Issue: If you try to copy a file to a directory that doesn't exist using 
shutil.copy, a IsADirectory error is raised saying the directory exists. 

This issue is actually caused when `open(not_a_dir, 'wb') is called on a 
non-existing dir.

Expected behaviour: Should instead raise NotADirectoryError
-

Steps to reproduce:

[nav] In [1]: import os
 ...: from pathlib import Path
 ...: from shutil import copy
 ...:
 ...: tmp_file = '/tmp/some_file.txt'
 ...: Path(tmp_file).touch()
 ...: nonexistent_dir = 'not_a_dir/'
 ...: assert not os.path.exists(nonexistent_dir)
 ...: copy(tmp_file, nonexistent_dir)
---
IsADirectoryError Traceback (most recent call last)
 in 
  7 nonexistent_dir = 'not_a_dir/'
  8 assert not os.path.exists(nonexistent_dir)
> 9 copy(tmp_file, nonexistent_dir)

~/miniconda3/lib/python3.7/shutil.py in copy(src, dst, follow_symlinks)
243 if os.path.isdir(dst):
244 dst = os.path.join(dst, os.path.basename(src))
--> 245 copyfile(src, dst, follow_symlinks=follow_symlinks)
246 copymode(src, dst, follow_symlinks=follow_symlinks)
247 return dst

~/miniconda3/lib/python3.7/shutil.py in copyfile(src, dst, follow_symlinks)
119 else:
120 with open(src, 'rb') as fsrc:
--> 121 with open(dst, 'wb') as fdst:
122 copyfileobj(fsrc, fdst)
123 return dst

IsADirectoryError: [Errno 21] Is a directory: 'not_a_dir/'

--
messages: 386932
nosy: jerpint
priority: normal
severity: normal
status: open
title: shutil.copy raises IsADirectoryError when the directory does not 
actually exist
versions: Python 3.7

___
Python tracker 
<https://bugs.python.org/issue43219>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com