Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change drives using python os?

I'm trying to change the current directory from C: to Y: I tried:

import os
os.chdir('Y:')

but I keep getting an error saying that it can't locate the drive. Essentially I'm looking for the equivalent of the

cd /d

command in cmd.

like image 393
aensm Avatar asked Jun 15 '12 19:06

aensm


People also ask

How do I change drives in Python?

To change the current working directory in Python, use the chdir() method. The method accepts one argument, the path to the directory to which you want to change. The path argument can be absolute or relative.

How do I change directory in Python os?

chdir() method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path. Parameters: path: A complete path of directory to be changed to new directory path.

How do I access D drive in Python?

In the System variable section, select "Path" and give Edit option. Now click New and type "D:\Python\"(for my example) in the new row. Click okay. This sets the python's path and now close all cmd sessions and in a new command prompt enter Python and the session opens.


1 Answers

Are you sure Y: really is a valid drive letter?

Try os.chdir('C:') and make sure that works. (It works for me.)

like image 108
Jon-Eric Avatar answered Sep 29 '22 18:09

Jon-Eric