Writing a one liner code that outputs filenames with more than 10 characters and whose content has more than 10 lines

57 Views Asked by At

Thank you all for the help before. I now had completed a task (so I thought) in order to achieve the following: I needed to write a one liner which outputs filenames that have more than 10 characters and also their contents consists out of more than 10 lines. My code is the following:

import os; [filename for filename in os.listdir(".") if len(filename) > 10 and len([line for line in open(filename)]) > 10 if filename =! filename.endswith(".ipynb_checkpoints")]

The problem that exists is the following error:

[Errno 13] Permission denied: '.ipynb_checkpoints'

I tried to exclude the .ipynb_checkpoint-files but it still doesn´t work properly. Do you have any suggestions on how to exclude them or solve my problem? Thank you for your time!

Greetings.

1

There are 1 best solutions below

0
35308 On BEST ANSWER

The issue might be the order of your if statements, String.ends with returns a boolean and cannot be compared to the string, and __pycache__ files also cause errors so I added it.

Some of the characters in ipynb files cause UnicodeDecodeError: 'charmap' codec can't decode errors and decoding in Latin-1 seemed to fix that. (UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to <undefined>)

you could try something like:

import os; print([filename for filename in os.listdir(".") if not filename.endswith(".ipynb_checkpoints") and not filename.endswith("__pycache__") and len(filename) > 10 and len([line for line in open(filename, encoding="Latin-1")]) > 10])

this solution seems to work but maybe a more general solution makes more sense.ie: PermissionError: [Errno 13] Permission denied