mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 06:46:18 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
30
MY_BOOK/ebook_src/manipulating_files/change_ext_file.py
Normal file
30
MY_BOOK/ebook_src/manipulating_files/change_ext_file.py
Normal file
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import os
|
||||
import sys
|
||||
import shutil
|
||||
|
||||
def change_file_ext():
|
||||
""" read a file and an extension from the command line and produces a copy with its extension changed"""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: change_ext.py filename.old_ext 'new_ext'")
|
||||
sys.exit()
|
||||
|
||||
name = os.path.splitext(sys.argv[1])[0] + "." + sys.argv[2]
|
||||
print (name)
|
||||
|
||||
try:
|
||||
shutil.copyfile(sys.argv[1], name)
|
||||
except OSError as err:
|
||||
print (err)
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
change_file_ext()
|
||||
|
||||
|
28
MY_BOOK/ebook_src/manipulating_files/count_unique_words_files.py
Executable file
28
MY_BOOK/ebook_src/manipulating_files/count_unique_words_files.py
Executable file
|
@ -0,0 +1,28 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import collections
|
||||
import string
|
||||
import sys
|
||||
|
||||
def count_unique_word_file():
|
||||
if len(sys.argv) < 2:
|
||||
print "Usage: python count_unique_word.py NAMEFILE"
|
||||
|
||||
words = collections.defaultdict(int)
|
||||
strip = string.whitespace + string.punctuation + string.digits + "\"'"
|
||||
for filename in sys.argv[1:]:
|
||||
with open(filename) as file:
|
||||
for line in file:
|
||||
for word in line.lower().split():
|
||||
word = word.strip(strip)
|
||||
if len(word) > 2:
|
||||
words[word] = +1
|
||||
for word in sorted(words):
|
||||
print("'{0}' occurs {1} times.".format(word, words[word]))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
count_unique_word_file()
|
15
MY_BOOK/ebook_src/manipulating_files/count_unique_words_frequency.py
Executable file
15
MY_BOOK/ebook_src/manipulating_files/count_unique_words_frequency.py
Executable file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
import collections
|
||||
import sys
|
||||
|
||||
def count_unique_word_freq():
|
||||
return collections.Counter(\
|
||||
sys.stdin.read().lower().split()).most_common(n)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
count_unique_word_freq()
|
27
MY_BOOK/ebook_src/manipulating_files/grep_word_from_files.py
Normal file
27
MY_BOOK/ebook_src/manipulating_files/grep_word_from_files.py
Normal file
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
import sys
|
||||
|
||||
def grep_word_from_files():
|
||||
''' using iterator enumerate to create a grep command '''
|
||||
word = sys.argv[1]
|
||||
for filename in sys.argv[2:]:
|
||||
with open(filename) as file:
|
||||
for lino, line in enumerate(file, start=1):
|
||||
if word in line:
|
||||
print("{0}:{1}:{2:.40}".format(filename, lino, line.rstrip()))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: grep_word_from_files.py word infile1 [infile2...]")
|
||||
sys.exit()
|
||||
else:
|
||||
grep_word_from_files()
|
||||
|
||||
|
||||
|
||||
|
54
MY_BOOK/ebook_src/manipulating_files/remove_blank_lines.py
Normal file
54
MY_BOOK/ebook_src/manipulating_files/remove_blank_lines.py
Normal file
|
@ -0,0 +1,54 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
|
||||
__author__ = "bt3"
|
||||
|
||||
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
def read_data(filename):
|
||||
lines = []
|
||||
fh = None
|
||||
try:
|
||||
fh = open(filename)
|
||||
for line in fh:
|
||||
if line.strip():
|
||||
lines.append(line)
|
||||
except (IOError, OSError) as err:
|
||||
print(err)
|
||||
finally:
|
||||
if fh is not None:
|
||||
fh.close()
|
||||
return lines
|
||||
|
||||
|
||||
def write_data(lines, filename):
|
||||
fh = None
|
||||
try:
|
||||
fh = open(filename, "w")
|
||||
for line in lines:
|
||||
fh.write(line)
|
||||
except (EnvironmentError) as err:
|
||||
print(err)
|
||||
finally:
|
||||
if fh is not None:
|
||||
fh.close()
|
||||
|
||||
|
||||
def remove_blank_lines():
|
||||
""" read a list of filenames on the command line and for each one produces another file with the same content but with no blank lines """
|
||||
|
||||
if len(sys.argv) < 2:
|
||||
print ("Usage: noblank.py infile1 [infile2...]")
|
||||
|
||||
for filename in sys.argv[1:]:
|
||||
lines = read_data(filename)
|
||||
if lines:
|
||||
write_data(lines, filename)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
remove_blank_lines()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue