forked from dd388/everybody-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
20180530Notes.txt
132 lines (101 loc) · 3.8 KB
/
20180530Notes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Everyone's notes/code snippets from the call will go here
### Bonnie's code
import os
def getDirectory():
targetDirectory = ('Enter the directory path: ')
return targetDirectory
def validateExtension(filename):
print("All files have extensions")
def validateFileSize(filename):
print("All files bigger than 0 bytes")
def findHiddenFiles(filename):
print("No hidden files found")
def findWhiteSpace(filename):
print("No filenames contain whitespace")
targetDirectory = getDirectory()
filenames = os.listdir(targetDirectory)
print(filenames)
for filename in filenames:
validateExtension(filename)
validateFileSize(filename)
findHiddenFiles(filename)
findWhiteSpace(filename)
print("Done!")
### Shira and Maggie's code
sourcefile = input('Enter the directory name or file name:')
# Does this check if sourcefile is a directory or a single file? source: https://docs.python.org/3.0/library/stat.html
import os, sys
from stat import *
def walktree(top, callback):
'''recursively descend the directory tree rooted at top,
calling the callback function for each regular file'''
for f in os.listdir(top):
pathname = os.path.join(top, f)
mode = os.stat(pathname)[ST_MODE]
if S_ISDIR(mode):
# It's a directory, recurse into it
walktree(pathname, callback)
elif S_ISREG(mode):
# It's a file, call the callback function
callback(pathname)
else:
# Unknown file type, print a message
print('Skipping %s' % pathname)
for file in sourcefile:
# below assumes that all file names are separated with a comma
# the below pulls out the file extension and checks that it is valid, i.e. between 2 and 4 characters long
try:
findext = sourcefile.find('.')
ext = sourcefile.find(',',findext) #how to find end of a single file name, if no comma is present to separate multiple files
fileext = sourcefile[findext:ext]
if
fileext == [2:4]
elseif:
print "Error: not a valid file extension"
print fileext
except:
print "Error: file does not have a file extension"
# the below searches for whitespace in filenames
space = sourcefile.find(' ')
' ' in sourcefile:
if True:
if False:
#finds whitespace in filenames with regular expressions
import re
try:
sp = re.compile('\s')
findsp = sourcefile.find(sp)
print "Error: file name contains whitespace"
# the below tries to check the file size in bytes and print the file size. If the file is 0 bytes, it returns an error. Source: https://stackoverflow.com/questions/6591931/getting-file-size-in-python
try:
def getSize(sourcefile):
st = os.stat(sourcefile)
if st.st_size == 0
print "Error: file is 0 bytes"
elseif:
print st.st_size
### farrell's code
#!/usr/bin/env python3
# attempt at reporting whether or not files in a directory have extensions or not.
import os, csv, argparse
parser = argparse.ArgumentParser()
parser.add_argument("-o","--output", help="Path to and filename for the CSV to create.")
parser.add_argument("-d","--directory", help="Path to input directory.")
args = parser.parse_args()
if args.output:
global csvOut
csvOut = args.output
if args.directory:
global inputDir
inputDir = args.directory
with open (csvOut, 'w') as f: #'wb' worked in python 2.7. Does not in 3.5...somethign to do with 'wb' being write+binary, so expecting binary values not strings.
writer = csv.writer(f)
writer.writerow(['path','filename','extension','extPassFail'])
for path, dirs, files in os.walk(inputDir):
for filename in files:
extension = os.path.splitext(filename)[1]
if extension == "":
extPassFail = "fail"
else:
extPassFail = "pass"
writer.writerow([path,filename,extension,extPassFail])