I'm writing a Python script that goes through a directory and gathers certain files, but there are a number of files I want excluded that all start the same.
Example code:
for name in files:
if name != "doc1.html" and name != "doc2.html" and name != "doc3.html":
print name
Let's say there are 100 hundred HTML files in the directory all beginning with 'doc'
. What would be the easiest way to exclude them?
Sorry I'm new to Python, I know this is probably basic.
Thanks in advance.
if not name.startswith('doc'):
print name
If you have more prefixes to exclude you can even do this:
if not name.startswith(('prefix', 'another', 'yetanother')):
print name
startswith can accept a tuple of prefixes.
for name in files:
if not name.startswith("doc"):
print name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With