【问题标题】:Referencing files located in another directory in program引用位于程序中另一个目录中的文件
【发布时间】:2011-09-26 22:54:59
【问题描述】:

我目前正在使用服务器。我创建了自己的目录,但需要访问服务器上不同目录中的文件。基本上,我的代码需要访问位于服务器上另一个目录中的文件,但我不知道如何去做。我的代码中是否有可以添加的命令,或者我可以在命令行中输入什么内容?我在代码中遇到的错误如下所示:

[aburac1][mcbbigram][/ruths/data/aburac1] python get-contribs.py
Traceback (most recent call last):
  File "get-contribs.py", line 96, in <module>
    fec_files = filter(lambda x: x.endswith('.fec'),os.listdir(dname))
OSError: [Errno 2] No such file or directory: '1001015'

文件 1001015 位于目录 /ruths/data/fec/efr 中。那么如何从我的目录中访问这些文件呢?

【问题讨论】:

    标签: python file directory


    【解决方案1】:

    您可以在输入文件名时指定整个路径。例如,假设您要打开目录“/home/docs”中的文件“foo.txt”。你可以运行命令:

    f = open("/home/docs/foo.txt")
    

    其中 f 现在是您的文件的句柄。或者,来自模块 os 的命令 os.chdir 更改当前工作目录。下面的sn -p 效果和上面一样:

    import os
    os.chdir("/home/docs")
    f = open("foo.txt")
    

    【讨论】:

      【解决方案2】:

      os.chdir("/ruths/data/fec/efr")

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-08
        • 2023-03-18
        • 2015-06-26
        • 2013-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-12
        相关资源
        最近更新 更多