Try to move files .csv but error

Hi!

I’m trying to move some csv files from a folder to another and it works, but at the end an error appears:

#importo tutte le librerie di cui necessito
import csv
from datetime import datetime, timedelta
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import shutil
import yfinance as yf
import os
import glob
import fnmatch
import smtplib, ssl
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication


titoli = ['A2A.MI', 'ABT.MI', 'ACE.MI', 'AC5.MI', 'AEF.MI', 'ADB.MI', 'ARN.MI', 'ALW.MI', 'ALK.MI', 'AMP.MI', 'ANIM.MI', 'AV.MI', 'ECNL.MI', 'ARIS.MI', 'ASC.MI', 'AUTME.MI', 'AVIO.MI', 'AZM.MI', 'BEC.MI', 'BFG.MI', 'BGN.MI', 'IF.MI', 'BMED.MI', 'BMPS.MI', 'PRO.MI', 'BST.MI', 'BAMI.MI', 'BAN.MI', 'B.MI', 'BDB.MI', 'BWZ.MI', 'BE.MI', 'BES.MI', 'BFF.MI', 'BIA.MI', 'BSS.MI', 'BIE.MI', 'BO.MI', 'BPSO.MI', 'BPE.MI', 'BRE.MI', 'BRI.MI', 'BC.MI', 'BZU.MI', 'CAI.MI', 'CLF.MI', 'CALT.MI', 'CPR.MI', 'CRL.MI', 'CELL.MI', 'CMB.MI', 'CEM.MI', 'CLI.MI', 'CIR.MI', 'CNS.MI', 'CLE.MI', 'COM.MI', 'CNF.MI', 'CE.MI', 'CSP.MI', 'CY4.MI', 'DIS.MI', 'DAN.MI', 'DAL.MI', 'DLG.MI', 'DIA.MI', 'DGV.MI', 'DIB.MI', 'DOV.MI', 'EDNR.MI', 'ELN.MI', 'ELC.MI', 'EM.MI', 'ENAV.MI', 'ENEL.MI', 'ENV.MI', 'ENI.MI', 'EQUI.MI', 'ERG.MI', 'PRT.MI', 'EUK.MI', 'ECMPM.MI', 'EGLA.MI', 'ETH.MI', 'XPR.MI', 'RACE.MI', 'YACHT.MI', 'FDA.MI', 'FM.MI', 'FILA.MI', 'FCT.MI', 'FF.MI', 'FBK.MI', 'FNM.MI', 'GAB.MI', 'GHC.MI', 'GSP.MI', 'GE.MI', 'GF.MI', 'G.MI', 'GEO.MI', 'GG.MI', 'GPI.MI', 'GTH.MI', 'GVS.MI', 'HER.MI', 'IGV.MI', 'IGD.MI', 'ILTY.MI', 'IMS.MI', 'DNR.MI', 'INDB.MI', 'ICOS.MI', 'IP.MI', 'ISP.MI', 'INW.MI', 'IRC.MI', 'IRE.MI', 'IEG.MI', 'ITW.MI', 'IG.MI', 'IDB.MI', 'ITM.MI', 'IVG.MI', 'IVS.MI', 'KME.MI', 'LNDR.MI', 'LDO.MI', 'LTMC.MI', 'LUVE.MI', 'LVEN.MI', 'MAIRE.MI', 'MARR.MI', 'MB.MI', 'MFEA.MI', 'MIT.MI', 'MONC.MI', 'MN.MI', 'MTV.MI', 'MOL.MI', 'NDT.MI', 'NWL.MI', 'NEXI.MI', 'NR.MI', 'OLI.MI', 'OJM.MI', 'ORS.MI', 'OVS.MI', 'PHN.MI', 'PHIL.MI', 'PIA.MI', 'PINF.MI', 'PVN.MI', 'PQ.MI', 'PIRC.MI', 'PLC.MI', 'PST.MI', 'PRY.MI', 'RWAY.MI', 'RAT.MI', 'RCS.MI', 'REC.MI', 'REY.MI', 'RST.MI', 'REVO.MI', 'SAB.MI', 'SG.MI', 'SFL.MI', 'SPM.MI', 'SCF.MI', 'SFER.MI', 'SL.MI', 'SRS.MI', 'IOT.MI', 'SERI.MI', 'SRI.MI', 'SES.MI', 'SIT.MI', 'SRG.MI', 'SFT.MI', 'SGF.MI', 'SOL.MI', 'SOM.MI', 'STLAM.MI', 'STMMI.MI', 'TIP.MI', 'TGYM.MI', 'TPRO.MI', 'TIT.MI', 'TEN.MI', 'TRN.MI', 'TES.MI', 'TSL.MI', 'TISG.MI', 'TNXT.MI', 'TOD.MI', 'TYA.MI', 'TB.MI', 'TXT.MI', 'UCG.MI', 'UD.MI', 'UNIR.MI', 'UNI.MI', 'VLS.MI', 'VIA.MI', 'ZUC.MI', 'WBD.MI', 'WIIT.MI', 'ZV.MI'] #è la lista dei titoli
for titolo in titoli:
    data = yf.download(titolo, period='15d') #scarico da yahoo finance i dati di tutti i titoli nella lista
    data.to_csv(titolo + ".csv")

src_dir = "/Users/fabry"
dst_dir = "/Users/fabry/Desktop/CSV"
for root, dirnames, filenames in os.walk(src_dir):
    for filename in fnmatch.filter(filenames, '*.csv'):
        shutil.move(os.path.join(root, filename),dst_dir

Error                                     Traceback (most recent call last)
<ipython-input-14-59331afb6fbd> in <module>
     27 for root, dirnames, filenames in os.walk(src_dir):
     28     for filename in fnmatch.filter(filenames, '*.csv'):
---> 29         shutil.move(os.path.join(root, filename),dst_dir)

/opt/anaconda3/lib/python3.8/shutil.py in move(src, dst, copy_function)
    787         real_dst = os.path.join(dst, _basename(src))
    788         if os.path.exists(real_dst):
--> 789             raise Error("Destination path '%s' already exists" % real_dst)
    790     try:
    791         os.rename(src, real_dst)

Error: Destination path '/Users/fabry/Desktop/CSV/SRG.MI.csv' already exists

But in practice SRG.MI is copied and if I remove it from the initial list, the same error appears with another one, and so on.

Why and what can I do to solve?

Thank you in advance.

As the error tell you the filename you are moving to already exists.

Its worth reading the documentation for shutil.move to understand what it is doing for you. That tells you that it will use os.rename.
The documentation for os.rename tell you that it will not overwrite an existing file.

If you know that this is ok then first you can delete the existsing file then do the move.

You can use os.remove to do this. Often coded like this to avoid the need to first check if the file exists:

try:
    os.remove(os.path.join(dst_dir, filename))
except FileNotFoundError:
    pass

Speculation. You’re walking the same directory tree that you’re copying to, so at some point you will descend to dst_dir and try to move a file over itself. To test this, catch the error and print root.

Thank you very much.
I tried but it’s the same. Now though it doesn’t move the files.

titoli = ['A2A.MI', 'ABT.MI', 'ACE.MI', 'AC5.MI', 'AEF.MI', 'ADB.MI', 'ARN.MI', 'ALW.MI', 'ALK.MI', 'AMP.MI', 'ANIM.MI', 'AV.MI', 'ECNL.MI', 'ARIS.MI', 'ASC.MI', 'AUTME.MI', 'AVIO.MI', 'AZM.MI', 'BEC.MI', 'BFG.MI', 'BGN.MI', 'IF.MI', 'BMED.MI', 'BMPS.MI', 'PRO.MI', 'BST.MI', 'BAMI.MI', 'BAN.MI', 'B.MI', 'BDB.MI', 'BWZ.MI', 'BE.MI', 'BES.MI', 'BFF.MI', 'BIA.MI', 'BSS.MI', 'BIE.MI', 'BO.MI', 'BPSO.MI', 'BPE.MI', 'BRE.MI', 'BRI.MI', 'BC.MI', 'BZU.MI', 'CAI.MI', 'CLF.MI', 'CALT.MI', 'CPR.MI', 'CRL.MI', 'CELL.MI', 'CMB.MI', 'CEM.MI', 'CLI.MI', 'CIR.MI', 'CNS.MI', 'CLE.MI', 'COM.MI', 'CNF.MI', 'CE.MI', 'CSP.MI', 'CY4.MI', 'DIS.MI', 'DAN.MI', 'DAL.MI', 'DLG.MI', 'DIA.MI', 'DGV.MI', 'DIB.MI', 'DOV.MI', 'EDNR.MI', 'ELN.MI', 'ELC.MI', 'EM.MI', 'ENAV.MI', 'ENEL.MI', 'ENV.MI', 'ENI.MI', 'EQUI.MI', 'ERG.MI', 'PRT.MI', 'EUK.MI', 'ECMPM.MI', 'EGLA.MI', 'ETH.MI', 'XPR.MI', 'RACE.MI', 'YACHT.MI', 'FDA.MI', 'FM.MI', 'FILA.MI', 'FCT.MI', 'FF.MI', 'FBK.MI', 'FNM.MI', 'GAB.MI', 'GHC.MI', 'GSP.MI', 'GE.MI', 'GF.MI', 'G.MI', 'GEO.MI', 'GG.MI', 'GPI.MI', 'GTH.MI', 'GVS.MI', 'HER.MI', 'IGV.MI', 'IGD.MI', 'ILTY.MI', 'IMS.MI', 'DNR.MI', 'INDB.MI', 'ICOS.MI', 'IP.MI', 'ISP.MI', 'INW.MI', 'IRC.MI', 'IRE.MI', 'IEG.MI', 'ITW.MI', 'IG.MI', 'IDB.MI', 'ITM.MI', 'IVG.MI', 'IVS.MI', 'KME.MI', 'LNDR.MI', 'LDO.MI', 'LTMC.MI', 'LUVE.MI', 'LVEN.MI', 'MAIRE.MI', 'MARR.MI', 'MB.MI', 'MFEA.MI', 'MIT.MI', 'MONC.MI', 'MN.MI', 'MTV.MI', 'MOL.MI', 'NDT.MI', 'NWL.MI', 'NEXI.MI', 'NR.MI', 'OLI.MI', 'OJM.MI', 'ORS.MI', 'OVS.MI', 'PHN.MI', 'PHIL.MI', 'PIA.MI', 'PINF.MI', 'PVN.MI', 'PQ.MI', 'PIRC.MI', 'PLC.MI', 'PST.MI', 'PRY.MI', 'RWAY.MI', 'RAT.MI', 'RCS.MI', 'REC.MI', 'REY.MI', 'RST.MI', 'REVO.MI', 'SAB.MI', 'SG.MI', 'SFL.MI', 'SPM.MI', 'SCF.MI', 'SFER.MI', 'SL.MI', 'SRS.MI', 'IOT.MI', 'SERI.MI', 'SRI.MI', 'SES.MI', 'SIT.MI', 'SRG.MI', 'SFT.MI', 'SGF.MI', 'SOL.MI', 'SOM.MI', 'STLAM.MI', 'STMMI.MI', 'TIP.MI', 'TGYM.MI', 'TPRO.MI', 'TIT.MI', 'TEN.MI', 'TRN.MI', 'TES.MI', 'TSL.MI', 'TISG.MI', 'TNXT.MI', 'TOD.MI', 'TYA.MI', 'TB.MI', 'TXT.MI', 'UCG.MI', 'UD.MI', 'UNIR.MI', 'UNI.MI', 'VLS.MI', 'VIA.MI', 'ZUC.MI', 'WBD.MI', 'WIIT.MI', 'ZV.MI'] #è la lista dei titoli
for titolo in titoli:
    data = yf.download(titolo, period='15d') #scarico da yahoo finance i dati di tutti i titoli nella lista
    data.to_csv(titolo + ".csv")


src_dir = "/Users/fabry"
dst_dir = "/Users/fabry/Desktop/CSV"
for root, dirnames, filenames in os.walk(src_dir):
    for filename in fnmatch.filter(filenames, '*.csv'):
        os.remove(os.path.join(dst_dir, filename))


try:
    os.remove(os.path.join(dst_dir, filename))
except FileNotFoundError:
    pass

Did I write correctly?
Thank you

As @steven.rumbalski said you also scan the target directory.
You will need to ignore dst_dir in your os.walk.

Recommend you print out the values of root as @steven.rumbalski suggested on each time around the loop to see this happen.

1 Like