简体   繁体   中英

os.chdir to a directory containing spaces/commas in its name

I faced a trouble. I have several directories then I need to cd to using os.chdir. But I keep getting errors in the next chunk of code:

import os
new_folder = 'Zarazogic acid A'
os.chdir(new_folder)
FileNotFoundError: [Errno 2] No such file or directory: 'Zarazogic acid A'

I tried to escape characters by:

new_directory = 'Zarazogic acid A'
new_directory = new_directory.replace(' ', '\ ')
os.chdir(new_directory)
FileNotFoundError: [Errno 2] No such file or directory: 'Zarazogi\\acid\\A'

Tried to escape with '\\' or even '\\\\' - still get the same error. How do I fix this?

Python 3 code

Here's a simple Python 3 script ( md23.py ) which shows that the directory isn't where you think it is.

#!/usr/bin/env python3

import os

new_folder = 'Zarazogic acid A'

print("0:", os.listdir('.'))
os.mkdir(new_folder)
print("1:", os.listdir('.'))
os.chdir(new_folder)
print("2:", os.listdir('.'))
os.chdir('..')
print("3:", os.listdir('.'))
os.rmdir(new_folder)
print("4:", os.listdir('.'))

It's not beautiful, but it works with Python 3 — you'd have to change the printing to make it work with Python 2.

When the script is in an otherwise empty directory, the output is:

0: ['md23.py']
1: ['md23.py', 'Zarazogic acid A']
2: []
3: ['md23.py', 'Zarazogic acid A']
4: ['md23.py']

This shows that the directory could be created, listed, changed to, that the new directory was empty, that it still existed when changing back up a directory level, and that it could be removed.

You should be able to take this, place it beside your current script, and run it, and it should succeed. If the Zarazogic acid A directory already exists, it will fail. For example, if I create the directory before running the script, I get this output:

$ mkdir 'Zarazogic acid A'
$ python3 md23.py
0: ['md23.py', 'Zarazogic acid A']
Traceback (most recent call last):
  File "md23.py", line 8, in <module>
    os.mkdir(new_folder)
FileExistsError: [Errno 17] File exists: 'Zarazogic acid A'
$

Python 2 code

A variant script, md29.py , with directory names printed too:

#!/usr/bin/env python2.7

import os

new_folder = 'Zarazogic acid A'


print "0:", os.getcwd(), os.listdir('.')
os.mkdir(new_folder)
print "1:", os.getcwd(), os.listdir('.')
os.chdir(new_folder)
print "2:", os.getcwd(), os.listdir('.')
os.chdir('..')
print "3:", os.getcwd(), os.listdir('.')
os.rmdir(new_folder)
print "4:", os.getcwd(), os.listdir('.')

Example run:

0: /Users/jleffler/soq/junk ['md23.py', 'md29.py']
1: /Users/jleffler/soq/junk ['md23.py', 'md29.py', 'Zarazogic acid A']
2: /Users/jleffler/soq/junk/Zarazogic acid A []
3: /Users/jleffler/soq/junk ['md23.py', 'md29.py', 'Zarazogic acid A']
4: /Users/jleffler/soq/junk ['md23.py', 'md29.py']

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM