[os.path.join(r'E:\Python', name) for name in []] returns []

Chris Angelico rosuav at gmail.com
Tue Jan 29 21:17:03 EST 2013


On Wed, Jan 30, 2013 at 12:56 PM, iMath <redstone-cold at 163.com> wrote:
> 在 2013年1月29日星期二UTC+8下午9时33分26秒,Steven D'Aprano写道:
>> iMath wrote: > why [os.path.join(r'E:\Python', name) for name in []] returns [] ? Because you are iterating over an empty list, []. That list comprehension is the equivalent of: result = [] for name in []: result.append( os.path.join(r'E:\Python', name) ) Since you iterate over an empty list, the body of the loop never executes, and the result list remains empty. What did you expect it to do? -- Steven
>
> just in order to get the full path name of each file .

Then it's done exactly what it should. It's given you the full path of
all of your list of zero files.

ChrisA



More information about the Python-list mailing list