1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Akado2009
  • 381
  • 1
  • 4
  • 11
  • 3
    Are you sure you're starting in the right directory, and that you actually have a folder named `Zarazogic acid A` in that directory? – user2357112 Feb 10 '17 at 22:12
  • 2
    On the face of it, the problem is just that in the directory which the process has as its current directory, there is no sub-directory `Zarazogic acid A`. The function is trying to change to the directory recognizing the spaces in the name; the directory isn't there. Check whether you're in the directory you think you're in — or use an absolute pathname to the directory. – Jonathan Leffler Feb 10 '17 at 22:13
  • @Wondercricket, sorry, but what do you mean by that? – Akado2009 Feb 10 '17 at 22:13
  • Try running `print(os.getcwd())` to see if you are in the directory you expect. – Dietrich Epp Feb 10 '17 at 22:14
  • @user3570029 they mean you might not be in the appropriate parent directory. One quick way, check if 'Zarazogic acid A' is in `os.listdir()`. Or check `os.getcwd()`. – juanpa.arrivillaga Feb 10 '17 at 22:15
  • Tbh, tried to emulate in in interpreter, the directory successfully changed as I can see by `os.getcwd())`, I used the directory name with spaces and etc. Why isn't it working in my script? – Akado2009 Feb 10 '17 at 22:16
  • What directory are you *running* your script from? Put `print(os.getcwd())` in your script... that should be illuminating. In other words, the working directory is where you run `python /path/to/my_script.py` not where `my_script.py` lives – juanpa.arrivillaga Feb 10 '17 at 22:16
  • @juanpa.arrivillaga running from `/home/prosvirov/new_pdb`, this folder contains `Zarazogic acid A` as it can be seen from `os.listdir()` – Akado2009 Feb 10 '17 at 22:19
  • Did you try printing those things in your script, and running them so that you reproduce the error? If indeed the current working director is `/home/prosvirov/new_pdb` and 'Zarazogic acid A` directory is printed in `os.listdir()` when you run your script, then that would be interesting... but please try to rule that out definitively - so not just from the interpreter, but exactly how you are running your script. – juanpa.arrivillaga Feb 10 '17 at 22:23
  • @juanpa.arrivillaga, changed all the path to absolute and now it's working. Figured out, that I misspelled one of the folder in script like a dumb. Anyway, thanks for the help! – Akado2009 Feb 10 '17 at 22:29

1 Answers1

0

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']
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278