Python3 batch rename - python

I'm new in python and trying to make script, which renames all files in directory by this pattern ShowName + SeasonNumber + EpisodeNumber
This script gets filename as argv[1] and directory as argv[2]. Then in find_needed_parts it parses string and returns number of seasone, episode and show name. But this script is droping with such traceback:
Traceback (most recent call last):
File "/home/usrname/Documents/renameFiles/main.py", line 38, in <module>
main()
File "/home/usrname/Documents/renameFiles/main.py", line 35, in main
os.rename(pathAndFilename, os.path.join(dir, rename_name))
File "/usr/lib/python3.5/posixpath.py", line 89, in join
genericpath._check_arg_types('join', a, *p)
File "/usr/lib/python3.5/genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'builtin_function_or_method'
And here is the code:
#!/usr/bin/python3
import glob
import os
import sys
import re
def find_needed_parts(filename):
ep_reg = re.search("(E|e)(\d+)", filename)
if ep_reg:
found_ep = ep_reg.group(2)
s_reg = re.search("(S|s)(\d+)", filename)
if s_reg:
found_s = s_reg.group(2)
ext_reg = re.search("\.(\w+)", filename)
if ext_reg:
ext = ext_reg.group(1)
body_reg = re.search("((^(.*?)([^E0-9])+)|(^(.*?)([^e0-9])))", filename)
if body_reg:
body = body_reg.group(1)
return body, found_ep, found_s, ext
def main():
filename = sys.argv[1]
direct = sys.argv[2]
body, ep, s, ext = find_needed_parts(filename)
pattern = "*." + ext
for pathAndFilename in glob.iglob(os.path.join(direct, pattern)):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
ep = int(ep) + 1 # type: int
rename_name = str(body + 'S' + s + 'E' + str(ep) + '.' + ext)
os.rename(pathAndFilename, os.path.join(dir, rename_name))
main()
UPD1.
here are the params
argv[1] = abcd E01 S09 ockoeko ko k.avi
argv[2] = .

the main problem is in the statement
os.rename(pathAndFilename, os.path.join(dir, rename_name))
where you are using dir built-in function, when you've probably meant
os.rename(pathAndFilename, os.path.join(direct, rename_name))

Related

Can't return value from function in Python

I'm trying to return a value called 'html' from a python function:
def loop_accounts(account_name, interactive):
regions = set_regions(account_name)
aws_env_list = os.path.join('..', '..', 'output_files', 'account_names_list', 'aws_kiki_page-' + today + '.csv')
# Set the output file
output_dir = os.path.join('..', '..', 'output_files', 'aws_instance_list', 'csv', '')
if interactive == 1:
output_file = os.path.join(output_dir, 'aws-instance-list-' + account_name + '-' + today +'.csv')
output_file_name = 'aws-instance-list-' + account_name + '-' + today + '.csv'
else:
output_file = os.path.join(output_dir, 'aws-instance-master-list-' + today +'.csv')
output_file_name = 'aws-instance-master-list-' + today +'.csv'
htmlfile, htmlfile_name, remove_htmlfile = convert_csv_to_html_table(output_file, today, interactive, account_name)
with open(htmlfile, 'r') as htmlfile:
html = htmlfile.read()
return html
But python isn't seeing the value html when I try to return it.
Traceback (most recent call last):
File ".\aws_ec2_list_instances_no_output.py", line 657, in <module>
main()
File ".\aws_ec2_list_instances_no_output.py", line 631, in main
html, htmlfile, output_file, output_file_name, remove_htmlfile = loop_accounts(aws_env_list, interactive, fieldnames, all_accounts_question)
File ".\aws_ec2_list_instances_no_output.py", line 594, in loop_accounts
return html, htmlfile, output_file, output_file_name, remove_htmlfile
UnboundLocalError: local variable 'html' referenced before assignment
Why is python not seeing this value called html? How can I return it?
html doesn't exist when interactive==1.
Depending on what you want to do, you might want to create html before the if else statement

Python 3: urlextract package, PermissionError

I am using Windows 10 x64, with Python 3.6.1 x86.
I have this script from a few months ago which was working fine, but right now it gives me a weird error. The script is a simple one that extract URLs from tweets saved in .csv files.
This is the script:
import datetime
from urlextract import URLExtract
twitter_files_list = ['File1.csv', 'File2.csv', 'File3.csv']
input_path = my_path
# Find domain of URL
def find_domain(url):
return url.split("//")[-1].split("/")[0]
# Clean domain from useless chars
def clean_domain(domain):
domain = domain.replace("[", "")
domain = domain.replace("]", "")
domain = domain.replace("\'", "")
return domain
# Extract URLs from Tweets
def url_extract(filename):
print('\n' + filename + ':')
url_counter = 0
url_file = open('extracted_urls/urls_' + filename, 'a')
# Open file
f = open(input_path + filename, "r", encoding="utf8")
lines = f.readlines()
# Search for contents of column "text"
text = []
for x in lines:
text.append(x.split('\t')[4])
# Close file
f.close()
extractor = URLExtract()
for i in range(len(text)):
try:
if extractor.find_urls(text[i]): # Check if URL exists
url = extractor.find_urls(text[i])
domain = find_domain(str(url))
if not " " in domain:
url_file.write(str(clean_domain(domain)) + "\n")
url_counter += 1
except 'Not Found':
continue
url_file.close()
# Main
if __name__ == '__main__':
print('\nURL Characterization:\n')
# Start timer
start = datetime.datetime.now()
# Find the unique usernames for every file
for twitter_file in twitter_files_list:
print('Searching ' + str(twitter_file) + '...')
url_extract(twitter_file)
# End timer
end = datetime.datetime.now()
# Print results
print("\nProcess finished")
print("Total time: " + str(end - start))
This gives me the following error:
Traceback (most recent call last):
File "C:/Users/Aventinus/url_analysis/url_extractor.py", line 77, in <module>
url_extract(twitter_file)
File "C:/Users/Aventinus/url_analysis/url_extractor.py", line 50, in url_extract
extractor = URLExtract()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\urlextract.py", line 65, in __init__
if not self._download_tlds_list():
File "C:\Program Files (x86)\Python36-32\lib\site-packages\urlextract.py", line 114, in _download_tlds_list
with open(self._tld_list_path, 'w') as ftld:
PermissionError: [Errno 13] Permission denied: 'C:\\Program Files (x86)\\Python36-32\\lib\\site-packages\\.tlds'
I have no idea how to interpret this.
you can try run the script as administrator

Python 2.7, TypeError: 'module' object is not callable (referring to command-line argument)

I've used command-line arguments with argparse many times, but I can't seem to figure out why I am getting a TypeError when trying to use these arguments. I have written a script that will take 5 required arguments. I've included the script below. The argument values are just several directories or file paths. Any idea what I am doing wrong here?
Here is the traceback:
Traceback (most recent call last):
File "C:/program.py", line 186, in <module>
sys.exit(builder.main())
File "C:/program.py", line 119, in main
filename_config = os.path(self.config)
TypeError: 'module' object is not callable
Here is the code:
import argparse
import sys
import os
import re
from tempfile import mkstemp
from shutil import move
from os import remove, close
class Builder():
def __init__(self):
parser = argparse.ArgumentParser(description='builder: ' + 'version 1.0.0')
parser.add_argument('-s', '--source', help="source dir", type=str)
parser.add_argument('-t', '--target', help="destination dir.", type=str)
parser.add_argument('-l', '--lua', help="path and filename of lua code", type=str)
parser.add_argument('-c', '--config', help="path and filename of configuration", type=str)
parser.add_argument('-d', '--documentation', help="path and filename of documentation", type=str)
args = parser.parse_args()
self.source = args.source
self.target = args.target
self.lua = args.lua
self.config = args.config
self.documentation = args.documentation
if not os.path.isdir((self.target)):
os.makedirs(self.target)
def replace_lua(self, filename, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, filename))
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_version(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def replace_documentation(self, filename, pattern, subst):
old_file_path = os.path.join(self.target, filename)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(old_file_path)
for line in old_file:
new_file.write(line.replace(pattern, subst))
#close temp file
new_file.close()
close(fh)
old_file.close()
#Remove original file
remove(old_file_path)
#Move new file
move(abs_path, os.path.join(self.target, filename))
def main_XXX(self):
if not os.path.isdir(self.target):
os.makedirs(self.target)
#lua pattern
pattern = "<script><![CDATA[]]></script>"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
read_lua = f.read()
f.closed
subst = "<script><![CDATA[\n" + read_lua + "\n]]></script>"
#version pattern
old_version = "<version>1</version>"
new_version = "<version>2</version>"
#documentation
old_doc = "<documentation></documentation>"
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
read_doc = f.read()
f.closed
read_doc = "<documentation>\n" + read_doc + "\n</documentation>"
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#file_path = os.path.join(subdir, file)
self.replace_lua(file, pattern, subst)
#replace_version(file, old_version, new_version)
self.replace_documentation(file, old_doc, read_doc)
def main(self):
#create expression objects
version = re.compile(r"^(\s*)<version>.*</version>\s*")
mod_date = re.compile(r"^(\s*)<modified>.*</modified>\s*")
lua_script = re.compile(r"^(\s*)<script>.*</script>\s*")
doc = re.compile(r"^(\s*)<documentation>.*</documentation>\s*")
#get version
filename_config = os.path(self.config)
with open(filename_config, 'r') as f:
for line in f:
m_obj = version.search(line)
if m_obj:
new_version = line
else:
m_obj = mod_date.search(line)
if m_obj:
new_mod_date = line + "\n"
f.closed
filename_doc = os.path(self.documentation)
with open(filename_doc, 'r') as f:
new_doc = f.read()
f.closed
new_doc = "<documentation>\n" + new_doc + "\n</documentation>\n"
filename_lua = os.path(self.lua)
with open(filename_lua, 'r') as f:
new_lua = f.read()
f.closed
#iterate
for subdir, dirs, files in os.walk(self.source):
for file in files:
print os.path.join(subdir, file)
#Create temp file
fh, abs_path = mkstemp()
new_file = open(abs_path,'w')
old_file = open(os.path.join(self.source, file))
for line in old_file:
m_obj = version.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_version)
else:
m_obj = mod_date.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_mod_date)
else:
m_obj = doc.search(line)
if m_obj:
new_file.write(m_obj.group(1) + new_doc)
else:
m_obj = lua_script.search(line)
if m_obj:
new_file.write(m_obj.group(1) + "<script><![CDATA[\n" + new_lua + "\n]]></script>\n")
else:
new_file.write(line)
#close temp file
new_file.close()
close(fh)
old_file.close()
#Move new file
move(abs_path, os.path.join(self.target, file))
if __name__ == "__main__":
builder = Builder()
sys.exit(builder.main())
path is a module within os module. You need to call it's member functions. I don't know which one exactly do you need, but there is a os.path module documetnation here: https://docs.python.org/2/library/os.path.html

Python error extracting some zip files

i wrote a small app that downloads a zip file (with a different extension) when provided by a link and extracts the file to a renamed folder.
For some reason its working for some of my zip files, but not for all of them.
I get a :
Traceback (most recent call last):
File "download_unzip.py", line 48, in <module>
main()
File "download_unzip.py", line 42, in main
shutil.move(unzip_file(temp_kmz),'temp_extracted/')
File "download_unzip.py", line 26, in unzip_file
fd = open(name, 'w')
IOError: [Errno 2] No such file or directory: 'models/model.dae'
My code is :
import sys , urllib , zipfile , os.path , argparse , shutil
parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()
print args.url
url = args.url
temp_kmz="temp_kmz"
def unzip_file(path):
zfile = zipfile.ZipFile(path)
extracted_filename = zfile.infolist()[0].filename[:-1]
for name in zfile.namelist():
(dirname, filename) = os.path.split(name)
#print "Decompressing " + filename + " on " + dirname
if filename == '':
# directory
if not os.path.exists(dirname):
os.mkdir(dirname)
else:
# file
fd = open(name, 'w')
fd.write(zfile.read(name))
fd.close()
zfile.close()
return extracted_filename
def download_file():
urllib.urlretrieve (url, temp_kmz)
return True
def main():
if (download_file()):
print "Now deleting temp..."
shutil.rmtree('temp_extracted/')
print "unzipping.. and renaming folder"
shutil.move(unzip_file(temp_kmz),'temp_extracted/')
print "Finished!!"
else:
print "Error downloading file"
main()
my working downloaded file:
python download_unzip.py "http://dl.dropbox.com/u/2971439/dae.kmz"
The one that is not working:
python download_unzip.py
"http://dl.dropbox.com/u/2971439/rally_car_youbeq.kmz"
Please note that both files extract properly with my OS (Ubuntu)
fixed my problem with some heavy code changes:
import urllib2 ,argparse, shutil, urlparse , os , zipfile, os.path
from zipfile import ZipFile as zip
parser = argparse.ArgumentParser(description="Download and Unzip")
parser.add_argument('url', help='The action to take (e.g. install, remove, etc.)')
args = parser.parse_args()
print args.url
url = args.url
temp_kmz="temp_kmz"
def extractAll(zipName):
z = zip(zipName)
for f in z.namelist():
if f.endswith('/'):
os.makedirs(f)
else:
z.extract(f)
def download(url, fileName=None):
def getFileName(url,openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to get filename from it
cd = dict(map(
lambda x: x.strip().split('=') if '=' in x else (x.strip(),''),
openUrl.info()['Content-Disposition'].split(';')))
if 'filename' in cd:
filename = cd['filename'].strip("\"'")
if filename: return filename
# if no filename was found above, parse it out of the final URL.
return os.path.basename(urlparse.urlsplit(openUrl.url)[2])
r = urllib2.urlopen(urllib2.Request(url))
try:
fileName = fileName or getFileName(url,r)
with open(fileName, 'wb') as f:
shutil.copyfileobj(r,f)
finally:
r.close()
def main():
download(url,temp_kmz)
extractAll(temp_kmz)
main()

Renaming OS files

I am trying to rename files based on their extensions. Below is my code, Somehow my os.rename isn't working. There aren't any errors though. I got no idea what is wrong. Hope you guys could help. Thanks.
import os
import glob
directory = raw_input("directory? ")
ext = raw_input("file extension? ")
r = raw_input("replace name")
pattern = os.path.join(directory, "*" + ext)
matching_files = glob.glob(pattern)
file_number = len(matching_files)
for filename in os.listdir(directory):
if ext in filename:
path = os.path.join(directory, filename)
seperated_names = os.path.splitext(filename)[0]
replace_name = filename.replace(seperated_names, r)
split_new_names = os.path.splitext(replace_name)[0]
for pad_number in range(0, file_number):
padded_numbers = "%04d" % pad_number
padded_names = "%s_%s" % (split_new_names, padded_numbers)
newpath = os.path.join(directory, padded_names)
newpathext = "%s%s" % (newpath, ext)
new_name = os.rename(path, newpathext)
I get an error:
directory? c:\breakup
file extension? .txt
replace name? test
Traceback (most recent call last):
File "foo.py", line 25, in <module>
new_name = os.rename(path, newpathext)
WindowsError: [Error 2] The system cannot find the file specified
shell returned 1
Anyhow, it looks like you're over complicating things. This works just fine:
import os
directory = raw_input("directory? ")
ext = raw_input("file extension? ")
r = raw_input("replace name? ")
for i, filename in enumerate(os.listdir(directory)):
if filename.endswith(ext):
oldname = os.path.join(directory, filename)
newname = os.path.join(directory, "%s_%04d%s" % (r, i, ext))
os.rename(oldname, newname)

Categories

Resources