Do I need to pass the full path of a file in another directory to open()?

后端 未结 5 2125
盖世英雄少女心
盖世英雄少女心 2020-12-05 03:58

I have a folder with ten files in it which I want to loop through. When I print out the name of the file my code works fine:

import os
indir = \'/home/des/te         


        
5条回答
  •  清歌不尽
    2020-12-05 04:47

    The examples to os.walk in the documentation show how to do this:

    for root, dirs, filenames in os.walk(indir):
        for f in filenames:
            log = open(os.path.join(root, f),'r')
    

    How did you expect the "open" function to know that the string "1" is supposed to mean "/home/des/test/1" (unless "/home/des/test" happens to be your current working directory)?

提交回复
热议问题