2

I'm making a script that will encode files within a directory using b64/b16 and I'm using os.listdir to do so, but it also lists directories which causes problems since now it's trying to encode directories as if it were a file.

How would I be able to exclude directories from os.listdir results?

import os
import sys
import base64
import codecs
import time
import string
import glob

#C:\\Users\\Fedora\\Desktop\\Win 10
path = "C:\\Users\\Fedora\\Desktop\\Win 10"
dirs = os.listdir(path)
files = []
filecount = 0
fileprogress = 0
for file in dirs:
   files.append(file)
   filecount = filecount + 1

for x in files:
    os.system("cls")
    fileprogress = fileprogress + 1
    print("File " + str(fileprogress) + "/" + str(filecount))
    print("Encrypting " + x + "...")
    inputfile = open(path + "\\" + x, "rb")
    data = inputfile.read()
    inputfile.close()
    data = base64.b16encode(data)
    data = base64.b64encode(data)
    data = base64.b16encode(data)
    data = base64.b64encode(data)
    data = base64.b16encode(data)
    outputfile = open(path + "\\" + x + ".crypt", "wb")
    outputfile.write(data)
    outputfile.close()
Shayna
  • 334
  • 5
  • 17
  • do you know the path/name of the module you want to exclude? – CIsForCookies May 24 '17 at 07:09
  • I'm trying to exclude directories from os.listdir results, not a specific directory, just directories in general if that's what you're asking. – Shayna May 24 '17 at 07:31
  • Yeah... I hoped you were trying to exclude a specific module, and if so, I think https://stackoverflow.com/a/1668289/3512538 could have helped, but I don't think it will do much good for your situation – CIsForCookies May 24 '17 at 07:35

3 Answers3

3

use filter

filepath = "C:\\Users\\Fedora\\Desktop\\Win 10"
dirs = os.listdir(path)
files = filter(lambda x:os.path.isfile(os.path.join(filepath, x)), dirs)

or list comprehension with os.path.isfile()

filepath = "C:\\Users\\Fedora\\Desktop\\Win 10"
dirs = os.listdir(path)
files = [x for x in dirs if os.path.isfile(os.path.join(filepath, x))]
Skycc
  • 3,496
  • 1
  • 12
  • 18
1

Instead of using os.listdir() your can use os.walk which will return separate list for files and directories

python-oswalk-example

import os

path = "C:\\Users\\Fedora\\Desktop\\Win 10"

for (path, dirs, files) in os.walk(path):
    print path
    print dirs
    print files

pythoncentral os-walk

#Import the os module, for the os.walk function
import os

#Set the directory you want to start from
path = "C:\\Users\\Fedora\\Desktop\\Win 10"
for dirName, subdirList, fileList in os.walk(path):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124
Yaron
  • 10,166
  • 9
  • 45
  • 65
  • I'm having issues with os.walk. It doesn't only list the files in the specified directory, it also checks the sub directories which isn't what I want to do. The format it lists it in seems to be some sort of json style format which also isn't what I want to do, i'd like the results to be in plain text. I made a test directory with one file in it, and instead of just returning the contents of the directory it returned the directory path and the filename in a strange format. >>>('C:\\Users\\Fedora\\Desktop\\Folder', [], ['Test.txt']) – Shayna May 24 '17 at 07:15
1

You can use os.path.isdir function to check if the current file is a directory.

Also, it is much better to use string formatting operations instead of string concatenation: not

print("File " + str(fileprogress) + "/" + str(filecount))

but

print("File {}/{}".format(fileprogress, filecount))

Such code is much easier to understand and modify.

Eugene Morozov
  • 15,081
  • 3
  • 25
  • 32
  • I just tried os.path.isdir and that also didn't work, for some reason it won't return true, it still returns false even when it is a directory. [The Code](http://i.imgur.com/ZLn0cHG.png) returns [this](http://i.imgur.com/IUmdKEp.png) even though "modded" and "original" are both directories, it also prints out "x6" which is only supposed to happen if it doesn't return true, but it clearly should. Am I using os.path.isdir wrong? – Shayna May 24 '17 at 07:30
  • Nevermind, I found out the problem. os.path.isdir wants a path, I forgot that I was giving it a filename not a whole path, I just entered the entire path instead and it works fine. Thank you. – Shayna May 24 '17 at 07:38