-1

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.

wovano
  • 4,543
  • 5
  • 22
  • 49
alxnom334
  • 33
  • 4
  • 1
    "write a one-liner in Python" seems to me to completely miss the point of Pythonic style. Just sayin'. – rici Nov 26 '22 at 20:32
  • Well that was my university task:( – alxnom334 Nov 26 '22 at 20:39
  • 1
    Why on earth would a university teach you this? – wovano Nov 26 '22 at 20:47
  • I totaly agree with you, I just wanna finish with this and move on with better coding classes :) That is my last taskd sheet – alxnom334 Nov 26 '22 at 20:48
  • 1
    Although maybe Stack Overflow should get the diploma then ;-) – wovano Nov 26 '22 at 20:53
  • @alxnom334: ok, check out [`os.access`](https://docs.python.org/3/library/os.html?highlight=os%20access#os.access). But read the note in the documentation about the check-before-use race condition. – rici Nov 26 '22 at 22:50

1 Answers1

1

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

35308
  • 575
  • 5
  • 17