2011-07-01 45 views
-1

我想改變python中的目錄。我第一次嘗試:爲什麼我不能在python中更改目錄?

>>> os.getcwd() 

,並得到:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'os' is not defined 

我又試圖

>>> os.chir('/directory/i/want/to/change/to') 

,但我仍然有

NameError: name 'os' is not defined 

如何定義的操作系統?

+2

http://docs.python.org/tutorial/modules.html –

+2

我建議您閱讀一本關於您在「潛入」之前使用的語言的書籍,它有助於避免很多陷阱:) – Joril

+0

深入Python http://diveintopython.org/是我最喜歡的Python書籍之一 –

回答

19

您需要先導入os模塊:

import os 
os.getcwd() 
+0

'51秒前'你贏了9秒!有一個upvote。 – Chriszuma

+2

@Chriszuma謝謝!儘管這是一個棘手的問題! :) – knatten

+4

哇,+進口操作系統 –

2

您需要導入的包

import os 
4

您已經導入os模塊?

import os 
print(os.getcwd()) 
+0

哎呀...謝謝! – user808545

相關問題