I’m having a syntax error with an if statement. It was working correctly with a different version, but I’m writing to an output log and I didn’t like how it would output for every file it checked, I want it to only write once if the file exists or not.
The first code below is the one that is not working, it says the the third file is an undefined variable (fifth line of code).
The second code block is how it was working before.
Anyone know how to structure this?
if any(file.endswith('.ppt') for file in os.listdir(scanDestinationPath)):
os.startfile(machineFolderDir + machineType + '\\' +
partNumber + ' REV ' + revisionNumber + '\\' +
file, 'print')
errorLog = open(logBookDir + 'log.txt', 'a+')
errorLog.write('\nA setup sheet called PROG' + programNumber +
' ' + partNumber + ' ' + revisionNumber +
'.ppt was printed.\n')
errorLog.close()
else:
errorLog = open(logBookDir + 'log.txt', 'a+')
m = ('The exception occurred in printDecoSetupSheet().There does not appear '
f'to be a .ppt setup sheet file in folder {partNumber} {revisionNumber} '
f'under {machineType}. Moving on...\n')
errorLog.write(m)
errorLog.close()
Second code block:
if file.endswith(".ppt"):
os.startfile(machineFolderDir + machineType + '\\' +
partNumber + ' REV ' + revisionNumber + '\\' +
file, 'print')
errorLog = open(logBookDir + 'log.txt', 'a+')
errorLog.write('\nA setup sheet called PROG' + programNumber +
' ' + partNumber + ' ' + revisionNumber +
'.ppt was printed.\n')
errorLog.close()
else:
errorLog = open(logBookDir + 'log.txt', 'a+')
m = ('The exception occurred in printDecoSetupSheet().There does not appear '
f'to be a .ppt setup sheet file in folder {partNumber} {revisionNumber} '
f'under {machineType}. Moving on...\n')
errorLog.write(m)
errorLog.close()
The traceback is:
Exception has occurred: NameError name 'file' is not defined
File "C:\Users\MacalusoC\Desktop\Technical Docs\TLC_Program_Release\Scripts\Program_Release_v4.py", line 348, in printDecoSetupSheet
file, 'print')
File "C:\Users\MacalusoC\Desktop\Technical Docs\TLC_Program_Release\Scripts\Program_Release_v4.py", line 835, in main
printDecoSetupSheet(scanDestinationPath)
File "C:\Users\MacalusoC\Desktop\Technical Docs\TLC_Program_Release\Scripts\Program_Release_v4.py", line 869, in <module>
main()
Related
I tried looking on stack overflow for a solution to this and other online resources but my specific situation didn't apply to what I found online.
When I run my script, it works until the line that I expect to create a tar archive of 2 files using the os.system command and store that archive in /home/dahmed26/backups/xmlfiles
It gives the following error:
tar: Cowardly refusing to create an empty archive
Try 'tar --help' or 'tar --usage' for more information.
sh: line 1: .tar.gz: command not found
This is my code:
import os
currentuser = os.popen('whoami')
username = username.strip()
if username != 'root':
print("Must be root")
exit()
else:
vmChoice = input("Choose a VM")
now = os.popen('date +%Y%m%d').read()
name = (vmChoice + '-' + now)
os.system('virsh dumpxml ' + vmChoice + ' > /home/dahmed26/backups/xmlfiles/' + vmChoice + '.xml')
location = os.popen('cat /home/dahmed26/backups/xmlfiles/' + vmChoice + '.xml | grep "source file" | cut -d "\'" -f2').read()
os.system('tar czf' + ' ' + '/home/dahmed26/backups/xmlfiles/' + name + '.tar.gz' + ' ' + '/home/dahmed26/backups/xmlfiles/' + vmChoice + '.xml' + location)
Problem: I wanted to get the data from the following url, however, I got the following error message.
I was wondering if you could guide me to fix my error. I appreciate your time!
import requests
import os
urls = {'1Q16':'https://f001.backblazeb2.com/file/Backblaze-Hard-Drive-Data/data_Q1_2016.zip'}
if not os.path.isdir('data'):
os.system('mkdir data')
for file in urls.keys():
if not os.path.exists('data/' + file):
os.system('mkdir ./data/' + file)
print('Requesting response from: ' + urls[file])
req = requests.get(urls[file])
print('Writing response to: /data/' + file + '/' + file + '.zip')
with open('data/' + file + '/' + file + '.zip', 'wb') as f:
f.write(req.content)
os.system('unzip ' + 'data/' + file + '/' + file + '.zip -d data/' + file + '/')
print('Unzipping data...')
os.system('rm ' + 'data/' + file + '/' + file + '.zip')
print(file + ' complete.')
print('------------------------------------------------------------------------------- \n')
Error messegae
Requesting response from: https://f001.backblazeb2.com/file/Backblaze-Hard-Drive-Data/data_Q1_2016.zip
Writing response to: /data/1Q16/1Q16.zip
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-9-251ee1e9c629> in <module>
9 req = requests.get(urls[file])
10 print('Writing response to: /data/' + file + '/' + file + '.zip')
---> 11 with open('data/' + file + '/' + file + '.zip', 'wb') as f:
12 f.write(req.content)
13
FileNotFoundError: [Errno 2] No such file or directory: 'data/1Q16/1Q16.zip'
The problem is your directory data/<file> is not being created and hence open() can not open a file since a part of path you provided does not exist. To ensure you have full compatibity while joining paths on python, you can use os.path.join(). For you, this would be:
import requests
import os
urls = {'1Q16':'https://f001.backblazeb2.com/file/Backblaze-Hard-Drive-Data/data_Q1_2016.zip'}
if not os.path.isdir('data'):
os.makedirs("data")
for file in urls.keys():
if not os.path.exists('data/' + file):
os.makedirs(os.path.join("data",file))
print('Requesting response from: ' + urls[file])
req = requests.get(urls[file])
print('Writing response to: /data/' + file + '/' + file + '.zip')
with open(os.path.join("data", file, file + '.zip', 'wb') as f:
f.write(req.content)
I'm trying to rename my media file names based on the metadata.
File name format is song name - artist name
import os
from tinytag import TinyTag
import re
for root, dirs, files in os.walk("C:/Users/username/Desktop/Music/"):
for name in files:
tag = TinyTag.get(root + "\\" + name)
if tag.artist != "":
if name.endswith((".mp3",".m4a")):
# try:
file_ext = os.path.splitext(name)[-1]
old_name = os.path.join(root, name)
new_name = re.sub(' +', ' ', os.path.join(root, tag.title + " - " + tag.artist + file_ext))
print(new_name)
os.rename(old_name, new_name)
# except:
# pass
Every file works except for Little Red Corvette by Prince:
C:/Users/username/Desktop/Music/1973 - James Blunt.mp3
C:/Users/username/Desktop/Music/Little Red Corvette - Prince .mp3
Traceback (most recent call last):
File "C:/Users/username/PycharmProjects/Practice/editAudioFileNames.py", line 15, in <module>
os.rename(old_name, new_name)
ValueError: rename: embedded null character in dst
What does the ValueError mean? I noticed that there is an extra space after Corvette. I did use re.sub in my code to trim the file names.
Ignore the try, except for now because the code does work with it. I could change the file name manually since this is the only one out of 850 songs but I want to know for my future understanding.
As a side note, this is my first ever useful code! Optimization critiques are most welcome.
Can you please try replacing these lines
old_name = os.path.join(root, name)
new_name = re.sub(' +', ' ', os.path.join(root, tag.title + " - " + tag.artist + file_ext))
with these lines
old_name = os.path.join(root, name.strip())
new_name = re.sub(' +', ' ', os.path.join(root, tag.title.strip() + " - " + tag.artist.strip() + file_ext.strip()))
Thanks
I have more than 500 xml files and each xml file should processed on FME workbench individually (iteration of FME workbench for each xml file).
For such a propose i have to run a python file (loop.py) to iterate FME workbench for each xml file.
The whole process was working in past on other PC without any problem. Now Once i run Module i got the following error:
Traceback (most recent call last):E:\XML_Data
File "E:\XML_Data\process\01_XML_Tile_1.py", line 28, in
if "Translation was SUCCESSFUL" in open(path_log + "\" + data + ".log").read():
IOError: [Errno 2] No such file or directory: 'E:\XML_Data\data_out\log_01\re_3385-5275.xml.log'
Attached the python code(loop.py).
Any help is greatly appreciated.
import os
import time
# Mainpath and Working Folder:
#path_main = r"E:\XML_Data"
path_main = r"E:\XML_Data"
teil = str("01")
# variables
path_in = path_main + r"\data_in\03_Places\teil_" + teil # "Source folder of XML files"
path_in_tile10 = path_main + r"\data_in\01_Tiling\10x10.shp" # "Source folder of Grid shapefile"
path_in_commu = path_main + r"\data_in\02_Communities\Communities.shp" # "Source folder of Communities shapefile"
path_out = path_main + r"\data_out\teil_" + teil # "Output folder of shapefiles that resulted from XML files (tile_01 folder)"
path_log = path_main + r"\data_out\log_" + teil # "Output folder of log files for each run(log_01 folder)"
path_fme = r"%FME_EXE_2015%" # "C:\Program Files\FME2015\fme.exe"
path_fme_workbench = path_main + r"\process\PY_FME2015.fmw" # "path of FME workbench"
datalists = os.listdir(path_in)
count = 0
# loop each file individually in FME
for data in datalists:
if data.find(".xml") != -1:
count +=1
print ("Run-No." + str(count) + ": with data " + data)
os.system (path_fme + " " + path_fme_workbench + " " + "--SourceDataset_XML"+ " " + path_in + "\\" + data + " " + "--SourceDataset_SHAPE" + " " + path_in_tile10 + " " + "--SourceDataset_SHAPE_COMU" + " " + path_in_commu + " " + "--DestDataset_SHAPE" +" " +path_out + " " +"LOG_FILENAME" + " " + path_log + "\\" + data + ".log" )
print ("Data processed: " + data)
shape = str(data[19:28]) + "_POPINT_CENTR_UTM32N.shp"
print ("ResultsFileName: " + shape)
if "Translation was SUCCESSFUL" in open(path_log + "\\" + data + ".log").read():
# Translation was successful and SHP file exists:
if os.path.isfile(path_out + "\\" + shape):
write_log = open(path_out + "\\" + "result_xml.log", "a")
write_log.write(time.asctime(time.localtime()) + " " + shape + "\n")
write_log.close()
print("Everything ok")
#Translation was successful, but SHP file does not exist:
else:
write_log = open(path_out + "\\" + "error_xml.log", "a")
write_log.write(time.asctime(time.localtime()) + " Data: " + shape + " unavailable.\n")
write_log.close()
# Translation was not successful:
else:
write_log = open(path_out + "\\" + "error_xml.log", "a")
write_log.write(time.asctime(time.localtime()) + " Translation " + Data + " not successful.\n")
write_log.close()
print ("Number of calculated files: " + str(count))
Most likely, the script failed at the os.system line, so the log file was not created from the command. Since you mentioned a different computer, it could be caused by many reasons, such as a different version of FME (so the environment variable %FME_EXE_2015% would not exist).
Use a workspace runner transformer to do this.
The FME version is outdated.so first check the version whether it is creating the problem.
subprocess.call(["C:/Program Files/fme/FMEStarter/FMEStarter.exe", "C:/Program Files/fme/fme20238/fme.exe", "/fmefile.fmw" "LOG_FILENAME","logfile"], stdin=None, stdout=None, stderr=None, shell=True, timeout=None)
I have a python script that outputs file with similar text:
1234\insert into\\default\e72303\FINISHED\False\23ms\N/A\37m10s\105\2017-
08-23 09:55:10.155407000\2017-08-23 09:55:10.178453000
This data is split by "\" and is imported to a table in hive database.
My issue that some of that data contains ^M carriage return character which splits up my data:
1234\INSERT INTO customer_touch.XXX_test_data_pickup^M
(^M
CI\default\e72303\FINISHED\False\331ms\0 / 0 ( 0%)\37m11s\0\2017-08-
23 09:55:08.066620000\2017-08-23 09:55:08.398299000
I need to remove ^M and have my data all together. I have tried dos2unix on the filename which does remove ^M but my data is still split.
Below is my code. I have crontab setup that outputs this into a text file
datanodes = ["https://XXXXXXX/",
"https://XXXXXXX"]
for i, datanode in enumerate(datanodes):
try:
response = requests.get(datanode + "queries?
json",auth=HTTPDigestAuth(XXX, XXX),verify='XXXX.pem')
data = response.json()
for query in data['completed_queries']:
print query['query_id'] + "\\" + query['stmt'][0:80] + "\\" + query['default_db'] + "\\" + query['effective_user'] + "\\" + query['state'] + "\\" + str(query['executing']) + "\\" + query['duration'] + "\\" + query['progress'] + "\\" + query['waiting_time'] + "\\" + str(query['rows_fetched']) + "\\" + query['start_time']+ "\\" + query['end_time']
except IOError as ioe:
print ioe
except Exception as e:
print(e)
I was able to remove ^M with replace('\r', '') per Charles Duffy's suggestion. I changed my code to query['stmt'][0:80].replace('\r', '')
Put a | tr -d \r at the end of your cron job.
https://ss64.com/bash/tr.html