I m new in python and also in stackoverflow even if I sometimes I read it for various issue…
These days I m approaching to python3 cause I would like to improve a simple procedure: everyday I need to download data from an ubuntu system to win 10 and create the relative path but this already happens and works fine.
I just want to add a function in this program (made from other people) and create a folder inside the folder created and named with the date of the day (so everyday this folder has a different name!).
import paramiko
import stat
import os
import pandas as pd
#from tqdm.notebook import tqdm
from tqdm import tqdm
import logging
from logging_config import getJupyterHandler
logging.basicConfig(level=logging.INFO, handlers=[getJupyterHandler()])
logging.getLogger('paramiko.transport').setLevel(logging.WARNING)
logging.getLogger('paramiko.transport.sftp').setLevel(logging.WARNING)
import datetime
import utils
root = '/...path'
date = None
overwrite_existing = False
if date is None:
date = datetime.date.today().strftime("%Y%m%d")
logging.info("Checking data for today")
else:
logging.warning("Manual date is set")
sftp = utils.getPandoraSFTP(ip='...')
dates = sftp.listdir(root)
if date not in dates:
logging.error("No folder found")
else:
logging.info("Folder found")
files, numberOfFiles = utils.getFileListToCopy(sftp, f"{root}/{date}", f"C:\data\to_upload\
{date}", f"C:\data\to_brighter\{date}", True)
logging.info("Download tags")
tags = {k:v for k,v in files.items() if 'tag/' in k}
if len(tags)>0:
for remote, local in tags.items():
if os.path.exists(local) == False or overwrite_existing:
sftp.get(remote, local)
logging.info("Create summary table")
folder = os.path.dirname(list(tags.values())[0])
df = pd.DataFrame(columns=['id', 'Time', 'type' ,'Function', 'Leveling', 'Weather',
'Illumination', 'visibility', 'Road Type', 'sky', 'other', 'Issue description', 'driver',
'tester'])
for file in os.listdir(folder):
if file.lower().endswith('csv'):
try:
df = df.append(pd.read_csv(f"{folder}\{file}", header=None,
names=df.columns))
except Exception as e:
logging.error(f"Unable to process tag: {file} due to {e}")
df['Location'] = ''
filename = folder.split('\')[-2]
summary_path = f'...'
df[['Time', 'Function', 'Road Type', 'Illumination', 'Weather', 'Location', 'Issue
description']].to_excel(summary_path, index=False, header=False)
logging.info("Table created on ")
else:
logging.warning(f"No tags found")
pbar = tqdm(files.items())
for remote, local in pbar:
pbar.set_postfix_str(os.path.basename(remote))
if os.path.exists(local) == False or overwrite_existing:
sftp.get(remote, local)
#making new folder
folder_n = "name of folder..."
os.chdir ("C:\data\to_upload\{date}") #choose directory
os.mkdir (folder_n)
How you can see in these lasts strings (#making new folder) I simply added a mkdir function for create the folder inside the folder {date}. Of course the error says that it does not find that path!
Could someone help me and suggest a way on how identify that folder?
Thanks
2
Answers
thank you very much, it works!!
So I missed that "f"! I was sure paths must be wrote just inside quotation but I will study in deep this aspect.
You wrote
but you wanted
That is, you want an f-string to interpolate the
date
variableso its value will become part of the string.
You’d be better off phrasing it as a raw r-string
rather than doubling the
backwhacks.
Better still, just use regular
/
slashes: