需求:Python count文件数量,不需要count文件夹。
Linux是python2, windows是python3
用Linux的命令是:ls -l | grep “^-” | wc -l,grep好之后count文件。
CMD转了一下,踩了一些坑,最后用了dir /b /a-d | find /c /v ""作为command。
转到Python里的时候遇到encoding error,发现是Python3的原因,还需要decode一下。
count = subprocess.check_output(‘dir /b /a-d | find /c /v “”’, shell=True, cwd = str(Path)).decode(‘utf-8’)
成功解决