New answers tagged

4

Usage considerations Given that file extensions actually mean nothing, and that a script that simply outputs lines with using namespace in its file argument (or stdin) would be more composable, I’d take the following approach: Have the script read its arguments as files, or stdin if none given. Then just search for using namespace and output line numbers. ...


7

Working directory os.path.dirname(os.path.realpath(__file__)) is not the current working directory. The current working directory is available via os.getcwd. Either you should call that instead, or maybe rename your variable. Semicolons are usually discouraged in Python, so you can drop one here: working_directory: str = os.path.dirname(os.path.realpath(...


5

Your file[-3:] == ".py" check is later that it should be. It is part of the check executed for every line of a file, instead of only being done once per file. You should use: for file in os.listdir(working_directory): if file[-3:] != ".py": ... Are there other files in the directory? Maybe a README, Makefile.mak or .gitignore? Maybe you ...


2

in my opinion, the code is good, but I have some suggestions. com.github.coderodde.RemoteFile class com.github.coderodde.RemoteFile#download(java.io.File) The URISyntaxException in the method com.github.coderodde.RemoteFile#download(java.io.File) is never thrown and can be removed. In my opinion, the logic of this method should be extracted to an external ...


3

Refactor the code so each file type gets it's own function for counting the words like excel_counter() and pdf_counter(). Then use a dict to map file extensions to the functions. Something like: def docx_counter(file): text = docx2txt.process(file) return len(text.split()) def txt_counter(file): f = open(file, "r") text = f.read() ...


3

I recommend using pathlib and Path objects instead of os, and you should use a context manager when manipulating files (e.g. with open("file.txt", "r") as file: ...). You also have a lot of repeating code when you're checking extensions, and you keep checking the rest of the if statements even if it's matched an earlier one. And the final else: pass does ...


Top 50 recent answers are included