Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore hidden files in python functions? [duplicate]

Tags:

python

While using os.path.getsize() and os.path.isfile, my script returns the .DS_Store file too which I do not need. How do I ignore those?

import os

root = "/Users/Siddhartha/Desktop/py scripts"
for item in os.listdir(root):
    if os.path.isfile(os.path.join(root, item)):
        print item
like image 437
user2063763 Avatar asked Mar 05 '13 23:03

user2063763


1 Answers

Assuming you want to ignore all files that start with .:

import os

root = "/Users/Siddhartha/Desktop/py scripts"
for item in os.listdir(root):
    if not item.startswith('.') and os.path.isfile(os.path.join(root, item)):
        print item
like image 96
Andrew Clark Avatar answered Nov 09 '22 08:11

Andrew Clark