I use PyCharm for my python program, and I wrote codes below:
def get_files_name():
root_dir = "/Volumes/NO NAME/"
for root, ds, fs in os.walk(root_dir):
for f in fs:
print(os.path.join(root_dir, f))
get_files_name()
for root, ds, fs in os.walk(other_dir):
pass
So I get a Warning text like "Shadows name 'ds' from outer scope". I know the effect of scope, but I still want to use the same code format like "for root, ds, fs in ...." at inner or outer of scope.
I have searched PEP8, however, I still don't know how to name variable in function normatively.
Could you give me some suggestion?
In general: just ignore the warning. It's just a warning, not an error. You have use for both global and local names that happen to match.
However, I'd not put an
os.walk()call at global scope anyway. I'd rather put that into a function too, which has the happy side-effect of the names you used no longer being globals.For example, you could use a
main()function:Generally speaking, you don't want to leave loop names like
root, ds, fsas globals in your module anyway. Those are implementation details, and should not become part of the public API of a module. If you have to use aforloop like that at the global scope, use_single-underscore prefixes on the names and consider deleting them after the loop withdel: