【问题标题】:os.path.join with multiple files doesn't work带有多个文件的 os.path.join 不起作用
【发布时间】:2019-04-26 16:06:27
【问题描述】:

我在以下方面需要一些帮助,但我似乎无法解决这个问题。

我需要使用path.join 来构建我想要的路径。

base_name = os.path.dirname(__file__)
input_file = apache.log
output_filename = os.path.join(base_name,"test_",input_file)

但我得到的是/Users/test/Dropbox/Python/apache.log 而不是/Users/test/Dropbox/Python/test_/apache.log 我不明白为什么......

谢谢

【问题讨论】:

  • 我明白了...test_/apache.log
  • 复制并粘贴您的真实代码。您忘记引用字符串,该代码不会执行。谁知道还有什么其他区别?
  • 是的,我忘了在字符串上加引号

标签: python python-3.x path


【解决方案1】:

为什么不直接将test_ 目录添加到 input_file var 中?

base_name = os.path.dirname(__file__)
input_file = "test_/apache.log"
output_filename = os.path.join(base_name, input_file)

【讨论】:

    【解决方案2】:

    怎么样:

    base_name = os.path.dirname(__file__)
    input_file = "apache.log"
    output_filename = os.path.join(base_name,"test_",input_file)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2014-12-17
      • 2013-12-08
      • 1970-01-01
      相关资源
      最近更新 更多