Home > Mobile >  Navigating between directories in python
Navigating between directories in python

Time:12-20

I run the following sequence of commands. The aim was moving between various directories and folders based on the original directory (so it does not need to be apriori coded):

path=os.getcwd()
os.chdir('..')
path2=os.getcwd()
path3=path2 ('\\mydir')
os.chdir('path3')

As a result I get an error:

The system cannot find the file specified: 'C:\\work_folder\\mydir'

The directory C:\work_folder\mydir exists in the system, so the problem is in my opinion in missinetrpretation of '\'.

Thus I tried to do the following:

path3=path3.replace(r'\\',r'\')

Again I am getting error:

SyntaxError: EOL while scanning string literal

I will apreciate any help in overcoming this problem. Thank you

CodePudding user response:

In python, instead of using \s in your directories, you can use /s instead and it'll still work.

CodePudding user response:

You can not use a single un-escaped backslash (\) in a raw string. Change the line to:

path3 = path3.replace(r'\\', '\\')

This will solve your second error (SyntaxError: EOL while scanning string literal).

  • Related