Printing output only once inside the loop - python-3.x

for i in os.listdir(path_1):
for j in os.listdir(path_2):
file_name = (j.split('.')[0])
if i.__contains__(file_name) and i.endswith('txt'):
txt_tym = os.path.getctime(path_1 + '/' + i)
log_tym = os.path.getctime(path_2 + '/' + j)
if txt_tym >= log_tym:
print('Issues found in: '+i)
else:
print('No issues found')
I'm using this program to compare timestamp between two files in two different directory, it has same names but different extension,I need to show the result in a text document.If there is an issue it will print Isues found in: filename.I need to print No issues found only if there is no single files with the issue,Im using else inside the loop and it prints multiple times.Please give some suggestions to this

Something like this should work:
issues_found = false
for i in os.listdir(path_1):
for j in os.listdir(path_2):
file_name = (j.split('.')[0])
if i.__contains__(file_name) and i.endswith('txt'):
txt_tym = os.path.getctime(path_1 + '/' + i)
log_tym = os.path.getctime(path_2 + '/' + j)
if txt_tym >= log_tym:
print('Issues found in: '+i)
issues_found = true
if not issues_found:
print('No issues found')

Related

Telnet.read_very_eager in FOR loop and the first often returns b' ' while the rest works fine

I'm using Telnetlib to control a VIAVI 5800.Here is my code.
And I log results of telnet.read_very_eager. Sometimes the first read returns b'', maybe say it occurs a lot. I really don't understand why only the first read has this problem while the others just work fine. I also tried adding more time after I finishing changing the time slot, this still happens.
import telnetlib
import time
from robot.libraries.BuiltIn import BuiltIn
for vc4 in range(vc4, vc4_end):
for tug3 in range(stm_start, tug3_end):
for tug2 in range(1, 8):
for tu12 in range(1, 4):
tu12_com = ":SENSE:SDH:DS1:E1:LP:C12:CHANNEL" + " " + str(tu12) + "\n"
tug2_com = ":SENSE:SDH:DS1:E1:LP:C2:CHANNEL" + " " + str(tug2) + "\n"
tug3_com = ":SENSE:SDH:DS1:E1:LP:C3:CHANNEL" + " " + str(tug3) + "\n"
vc4_com = ":SENSE:SDH:CHANNEL:STMN" + " " + str(vc4)
tn.write(tu12_com.encode('ascii')) # change tu12
time.sleep(0.1)
tn.write(tug2_com.encode('ascii')) # change tug2
time.sleep(0.1)
tn.write(tug3_com.encode('ascii')) # change tug3
time.sleep(0.1)
tn.write(vc4_com.encode('ascii')) # change vc4
time.sleep(1.5)
tn.write(b":ABOR\n")
time.sleep(1)
tn.write(b":INIT\n")
time.sleep(5)
tn.write(b":SENSE:DATA? TEST:SUMMARY\n")
time.sleep(2)
result = tn.read_very_eager()
result_num = str(vc4) + "-" + str(tug3) + "-" + str(tug2) + "-" + str(tu12)
BuiltIn().log_to_console(result_num)
BuiltIn().log_to_console(result)
The results are like under:
results saved in excel workbook
results in RF Ride console
I'm so confused and wondering can anyone explain this. Thanks a lot.
BTW, my python version is:
C:\Users\Quinn>python -V
Python 3.7.9

How to create folder tree from list with paths

I am trying to put every path of a folder structure I need to be created into a list and then create all of them with os.makedirs() but something goes wrong. Only the Head-Folders are created, not the Sub-Folders.
def output_folders(trcpaths):
#trcpath is a list with several paths, example: ['/home/usr/folder1', '/home/usr/folder2']
global outputfolders
outputfolders = []
#Create Paths
for x, j in enumerate(trcpaths):
for i in os.listdir(trcpaths[x]):
if i.endswith('trc'):
folderpath1 = (j + '/' + i).split('.')[0] #/home/usr/folder1/outputfolder
folderpath2 = folderpath1 + '/Steps' #/home/usr/folder1/outputfolder/Steps
folderpath3 = folderpath2 + '/Step_1' #/home/usr/folder1/outputfolder/Steps/Step_1
folderpath4 = folderpath2 + '/Step_2'
folderpath5 = folderpath2 + '/Step_3'
folderpath6 = folderpath2 + '/Step_4'
folderpath7 = folderpath2 + '/Threshold'
outputfolders.append(folderpath1)
outputfolders.append(folderpath2)
outputfolders.append(folderpath3)
outputfolders.append(folderpath4)
outputfolders.append(folderpath5)
outputfolders.append(folderpath6)
outputfolders.append(folderpath7)
#Create Folders
for j, i in enumerate(outputfolders):
print(i)
if os.path.exists(i):
if j == 0:
input('The Output-Folder already exists! Overwrite?' )
shutil.rmtree(i)
os.makedirs(i)
Although when I print(i) the right folderpaths are printed but only the "Head-Folderpaths" are created like /home/usr/folder1/outputfolder and not all subsequent folderpaths. Why so?
This is what I get:
/home/usr/folder1/outputfolder
/home/usr/folder2/outputfolder
But this is what I need:
/home/usr/folder1/outputfolder
/home/usr/folder1/outputfolder/Steps
/home/usr/folder1/outputfolder/Steps/Step_1
/home/usr/folder1/outputfolder/Steps/Step_2
/home/usr/folder1/outputfolder/Steps/Step_3
/home/usr/folder1/outputfolder/Steps/Step_4
/home/usr/folder1/outputfolder/Steps/Threshold
/home/usr/folder2/outputfolder
/home/usr/folder2/outputfolder/Steps
/home/usr/folder2/outputfolder/Steps/Step_1
/home/usr/folder2/outputfolder/Steps/Step_2
/home/usr/folder2/outputfolder/Steps/Step_3
/home/usr/folder2/outputfolder/Steps/Step_4
/home/usr/folder2/outputfolder/Steps/Threshold
to keep your logic and your coding, with this code:
for j, i in enumerate(outputfolders):
print(i)
if os.path.exists(i):
if j == 0:
input('The Output-Folder already exists! Overwrite?' )
shutil.rmtree(i)
os.makedirs(i)
you dont create folder..you only delete the existing folder and recreate if it already exists
i'll add else to complete the operation:
for j, i in enumerate(outputfolders):
print(i)
if os.path.exists(i):
if j == 0:
input('The Output-Folder already exists! Overwrite?' )
shutil.rmtree(i)
os.makedirs(i)
else:
os.makedirs(i)
Try this (Tested on my Windows machine but should work on Linux as well)
import os
NUM_OF_STEPS = 5
def make_output_folders(trc_paths):
output_folders = []
for idx, path in enumerate(trc_paths):
for leaf in os.listdir(path):
if leaf.endswith('trc') and os.path.isdir(os.path.join(path, leaf)):
trc_folder = os.path.join(path, leaf)
output_folders.append(os.path.join(trc_folder, 'output_folder', 'Steps'))
steps_folder = output_folders[-1]
for x in range(1, NUM_OF_STEPS):
output_folders.append(os.path.join(steps_folder, 'Step_{}'.format(x)))
output_folders.append(os.path.join(trc_folder,'output_folder', 'Threshold'))
for _path in output_folders:
print(_path)
if not os.path.exists(_path):
os.makedirs(_path)
output_folders = []
# 'folder_1' contains a sub folder named '1_trc'
# 'folder_2' contains a sub folder named '2_trc'
make_output_folders(['c:\\temp\\55721430\\folder1', 'c:\\temp\\55721430\\folder2'])

Python save panda to csv files separately within FOR loop

I would like to save down some panda dataframe downloads separately as csv files. I am getting an error in the last line.
Is the syntax off?
Kind regards
sortedByISIN = pd.DataFrame()
for i in data['isin'].unique():
print('Adding ' + i)
d1 = data[data['isin'] == i]
d1['next_signal'] = d1['signal'].shift(-1)
#Shift x periods in the future
d1['futprice'] = d1['mid'].shift(-6)
d1['futT'] = d1['creationTimeStamp'].shift(-6)
d1['move'] = d1.apply(lambda row: (row['futprice'] - row['mid'])/row['mid'] * 10000 if row['futT'] - row['creationTimeStamp'] < 300000 else 0, axis=1)
d1['signal_transition'] = d1['next_signal'] - d1['signal']
sortedByISIN = sortedByISIN.append(d1)
sortedByISIN['period'] = np.floor(sortedByISIN.creationTimeStamp/3600000)
sortedByISIN.to_csv('Book'%i.csv')
or you can use also:
sortedByISIN.to_csv('Book' + str(i) + '.csv')
Use format:
sortedByISIN.to_csv('Book{}.csv'.format(i))
And for python 3.6+ is possible use f-strings:
sortedByISIN.to_csv(f'Book{i}.csv')

Proper placement of test within try/except loop (Python3)

I use a python script to webscrape for "Show Notes" and an mp3. When I encounter a page that has no show notes, this means the show was a Best Of, so I want to skip the download of the notes and mp3. I am not sure where the best place to insert the test would be. The snippet is as follows:
for show_html in showpage_htmls:
try:
p_html = s.get(show_html)
p_soup = BeautifulSoup(p_html.content, 'html.parser')
# set title for SHOW NOTES
title = ''
title = p_soup.title.contents[0]
# get SHOW NOTES chunk and remove unwanted characters (original mp3notes not changed)
mp3notes = ''
mp3notes = p_soup.find('div', {'class': 'module-text'}).find('div')
mp3notes = str(title) + str('\n') + str(mp3notes).replace('<div>','').replace('<h2>','').replace('</h2>','\n').replace('<p>','').replace('<br/>\n','\n').replace('<br/>','\n').replace('</p>','').replace('</div>','').replace('\u2032','')
# FIXME need to skip d/l if no notes
# set basename, mp3named and mp3showtxt
mp3basename = '{0}{1}{2}'.format(show_html.split('/')[3],show_html.split('/')[4],show_html.split('/')[5])
if (os.name == 'nt'):
mp3showtxt = mp3dir + '\\' + mp3basename + '.txt'
mp3named = mp3dir + '\\' + mp3basename + '.mp3'
else:
mp3showtxt = mp3dir + '/' + mp3basename + '.txt'
mp3named = mp3dir + '/' + mp3basename + '.mp3'
# save show notes to local
with open(mp3showtxt, 'w') as f:
try:
f.write(mp3notes)
print("Show notes " + mp3basename + " saved.")
except UnicodeEncodeError:
print("A charmap encoding ERROR occurred.")
print("Show notes for " + mp3basename + ".mp3 FAILED, but continuing")
finally:
f.close()
# FIXME need eyed3 to set mp3 tags since B&T are lazy
# get Full Show mp3 link
mp3url = p_soup.find('a', href = True, string = 'Full Show').get('href')
# get and save mp3
r = requests.get(mp3url)
with open(mp3named, 'wb') as f:
f.write(r.content)
print("Downloaded " + mp3basename + ".mp3.")
except AttributeError:
print(show_html + " did not exist as named.")
I would think an
if not (len(mp3notes) >= 50)
would work; just not sure where to put it or there is better way (more Pythonic).
Ideally, if the mp3notes are less than expected, no notes or mp3 for that show_html would be saved, and the script would start at the next show_html page.
Since I am new to Python, feel free to offer suggestions to making this more Pythonic as well; I am here to learn! Thanks.

Script to rename and copy files to a new directory.

Hi I have recently made this script to rename files I scan for work with a prefix and a date. It works pretty well however it would be great if it could make a directory in the current directory with the same name as the first file then move all the scanned files there. E.g. First file is renamed to 'Scanned As At 22-03-2012 0' then a directory called 'Scanned As At 22-03-2012 0' (Path being M:\Claire\Scanned As At 22-03-2012 0) is made and that file is placed in there.
I'm having a hard time figuring out the best way to do this. Thanks in advance!
import os
import datetime
#target = input( 'Enter full directory path: ')
#prefix = input( 'Enter prefix: ')
target = 'M://Claire//'
prefix = 'Scanned As At '
os.chdir(target)
allfiles = os.listdir(target)
count = 0
for filename in allfiles:
t = os.path.getmtime(filename)
v = datetime.datetime.fromtimestamp(t)
x = v.strftime( ' %d-%m-%Y')
os.rename(filename, prefix + x + " "+str(count) +".pdf")
count +=1
Not quite clear about your requirement. If not rename the file, only put it under the directory, then you can use the following codes (only the for-loop of your example):
for filename in allfiles:
if not os.isfile(filename): continue
t = os.path.getmtime(filename)
v = datetime.datetime.fromtimestamp(t)
x = v.strftime( ' %d-%m-%Y')
dirname = prefix + x + " " + str(count)
target = os.path.join(dirname, filename)
os.renames(filename, target)
count +=1
You can check help(os.renames).

Resources