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

后端 未结 5 2104
盖世英雄少女心
盖世英雄少女心 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:40

    You have to specify the path that you are working on:

    source = '/home/test/py_test/'
    for root, dirs, filenames in os.walk(source):
        for f in filenames:
            print f
            fullpath = os.path.join(source, f)
            log = open(fullpath, 'r')
    

提交回复
热议问题