Python split string to list function
Python split string to list function
1 2 | def split_string(string, delimeter):
return string.split(delimeter)
|
Explore python code snippets and tutorials
Python split string to list function
1 2 | def split_string(string, delimeter):
return string.split(delimeter)
|
Copy dict to a new object
1 2 3 4 5 6 7 8 9 10 11 12 13 | def copy_obj(old, excluded_attrs=[]):
'''
Returns a new dict with items for copy to a new object
excluded_attrs is list of attributes that we want to exclude for copy
'''
d = {}
for key, value in old.items():
if(len(excluded_attrs)>0):
if key not in excluded_attrs:
d[key] = value
else:
d[key] = value
return d
|
Remove duplicate files in python
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 | import os
import hashlib
path = u'/path/to/folder'
def file_as_bytes(file):
with file:
return file.read()
hashes = []
fname = []
files = []
for root,d_names,f_names in os.walk(path):
for f in f_names:
for name in f_names:
if name in files:
print(name)
files.append(name)
fname.append(os.path.join(root, f))
hash_string = hashlib.md5(file_as_bytes(open(os.path.join(root, f), 'rb'))).hexdigest()
hashes.append(hash_string)
multiple_item = list(set([x for x in hashes if hashes.count(x) > 2]))
for file_path in fname:
hash_string = hashlib.md5(file_as_bytes(open(file_path, 'rb'))).hexdigest()
if hash_string in multiple_item:
print('hash_string: {} exists for file {}'.format(hash_string, file_path))
# os.remove(file_path)
|
<p>The function <code>delete_directory_contents</code> you provided is designed to delete all the contents (files and subdirectories) within a specified directory. It uses the <code>os</code> and <code>shutil</code> modules in Python to perform …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import os,shutil
def delete_direcory_contents(directory_name):
'''
Delete directory contents
'''
for filename in os.listdir(directory_name):
file_path = os.path.join(directory_name, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
|
Random dates in python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import datetime
import random
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = random.randrange(int_delta)
return start + datetime.timedelta(seconds=random_second)
end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=60)
random_date(start_date, end_date)
|