Search code examples
pymol

How to execute a range expression for delete command in Pymol?


I am using PyMol and as of now I have to write a long command to delete useless files after splitting states:

split_states posesD01;

delete posesD01_0002;delete posesD01_0003;delete posesD01_0004;delete posesD01_0005;delete posesD01_0006;delete posesD01_0007;delete posesD01_0008;delete posesD01_0009;

I want to avoid deleting posesD01_0001

I have tried reducing it using regular expressions to:

split_states posesD01;

delete posesD01_000[2-9];

But it doesn't do anything. It does not throw any errors but it also does not do anything.

Any suggestions are welcome. Thank You for reading!


Solution

  • The PyMOL prompt accepts wildcards (*). If that fits you, it would be the easiest approach.

    delete posesD01_000*
    

    For a more fine-tuned selection of numbers a pure python loop can be used.

    for i in range(2, 10): cmd.delete(f"posesD01_000{i}")
    

    To take care of leading zeros (in the case of a wider range), a explicit formatting must be set.

    for i in range(0, 50): cmd.delete(f"posesD01_00{i:02d}")
    

    By the way, you don't need to write a semicolon at the end of each command. This is python! :)