【发布时间】:2013-10-30 20:27:13
【问题描述】:
import os
import random
os.chdir("C:\Users\Mainuser\Desktop\Lab6")
#Am i supposed to have a os.chdir?
# I think this is what's giving the error
#how do i fix this?
def getDictionary():
result = []
f = open("pocket-dic.txt","r")
for line in f:
result = result + [ line.strip() ];
return result
def makeText(dict, words=50):
length = len(dict)
for i in range(words):
num = random.randrange(0,length)
words = dict[num]
print word,
if (i+1) % 7 == 0:
print
当我的桌面上明显有一个具有该名称的文件夹时,Python 给我一个错误,说它找不到指定的路径。可能是os.chidr??我做错了什么?
【问题讨论】:
-
始终使用原始字符串:
r"C:\Users\Mainuser\Desktop\Lab6" -
了解相对寻址。这将允许文件位于 `C:\Users\Mainuser\Desktop` 之外的某个位置
-
@hcwhsa:对这个相当直率的评论稍微解释一下?