Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python read all files in a folder except a file named "xyz"

Tags:

python

I want to read all files in a folder except a file named "xyz". When I reach to this file, I want to skip it and read the next one.

Currently I have the following code:

for file in glob.glob('*.xml'):
    data = open(file).read()
    print(file)

Obviously, this will read all files in that folder. How should I skip the file "xyz.xml"

like image 755
ahri Avatar asked May 19 '26 06:05

ahri


2 Answers

The continue keyword is useful for skipping an iteration of a for loop:

for file in glob.glob('*.xml'):
    if file=="xyz.xml":
        continue
    data = open(file).read()
    print(file)
like image 198
Andrew Johnson Avatar answered May 20 '26 18:05

Andrew Johnson


for file in [f for f in glob.glob('*.xml') if f != "xyz.xml"]:
    do_stuff()
like image 32
heltonbiker Avatar answered May 20 '26 18:05

heltonbiker