相关疑难解决方法(0)

Python的os.makedirs在我的路径中不理解"〜"

在我的路上,我有一点问题.

这个代码示例创建了一些名为"〜/ some_dir"的目录,并且不明白我想在我的主目录中创建some_dir.

my_dir = "~/some_dir"
if not os.path.exists(my_dir):
    os.makedirs(my_dir)
Run Code Online (Sandbox Code Playgroud)

请注意,这是基于Linux的系统.

python path

137
推荐指数
3
解决办法
4万
查看次数

Python path.exists()返回False

我正在构建一个基本文件服务器,我的程序找不到文件.

def sendfile(sock, myfile):
    print 'Serving file:', myfile
    print 'File exists?:', os.path.exists(myfile)

    path = os.path.normpath(os.path.join(os.getcwd(), myfile))
    print 'Serving file:', path
    print 'File exists?:', os.path.exists(path)
Run Code Online (Sandbox Code Playgroud)

即使'myfile'和'path'正确[文件与服务器程序位于同一目录],它们总是返回False.

IDLE工作正常,但没有传递给函数.

>>> print os.path.exists("/user/server/foo.txt")  
True
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

[编辑:]输出:

Serving file: foo.txt

File exists?: False
Serving file: /user/server/foo.txt

File exists?: False
Run Code Online (Sandbox Code Playgroud)

python file

12
推荐指数
3
解决办法
2万
查看次数

标签 统计

python ×2

file ×1

path ×1