Having trouble with Path.mkdir() using:
Path('C:\\Users\\', user, 'Desktop\\py\\', folder, '\\', str(x).rstrip('.bmp')).mkdir()
ignores its path and makes the directory at C:/ such as in the following:
"C:/directory_created_here"
rather than:
"C:/Users/user/Desktop/py/folder/directory__created_here"
You're not supposed to have \\\\
between your path segments. pathlib
handles that part. What you've done causes Python to take the '\\\\'
segment as the start of the path, discarding everything before it (except the C:
drive setting).
Also, rstrip('.bmp')
doesn't do what you think it does - it strips all .
, b
, m
, and p
characters from the right side of the string, rather than discarding a trailing .bmp
.
Your call should look something like
Path('C:\\Users', user, 'Desktop\\py', folder, str(x)).with_suffix('').mkdir()
or
Path('C:\\Users', user, 'Desktop\\py', folder, x).with_suffix('').mkdir()
if x
is already a string.
You could also try something like so
import os
user = 'my_name'
folder = 'new_folder'
x = 'test.bmp'
path_parts = [
'C:',
'Users',
user,
'Desktop',
'py',
folder,
str(x).rstrip('.bmp'),
]
path = os.path.join(*path_parts)
os.makedirs(path)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.