how to delete a file 3 months after downloading it in python
0
0
Entering edit mode
3 months ago
Debut ▴ 10

I have a script that allows to download several files from a URL. These files are placed directly in a folder that takes the name of the species entered by the user. I would like after 3 months this folder to be deleted in an automatic way.need help please. I don't know how I can position "remove" here is a part of the script:

species = input("Bacteria species ? : ")
TypeSeq= input ("fna ? or faa ? :")   

species = input("Bacteria species ? : ")
TypeSeq= input ("fna  ? ou faa  ?")   

if data["#Organism/Name"].str.contains(species, case = False).any():

    print(data.loc[data["#Organism/Name"].str.contains(species, case = False)]['Status'].value_counts())  
    FTP_list = data.loc[data["#Organism/Name"].str.contains(species, case = False)]["FTP Path"].values

if  TypeSeq == "faa" :

    if not os.path.exists(species):
        os.makedirs(species)

    for url in FTP_list:
        try : 
            parts = urllib.parse.urlparse(url)
            parts.path
            posixpath.basename(parts.path)
            suffix = "_protein.faa.gz"
            prefix = posixpath.basename(parts.path) 
            print(prefix+suffix)

            path = posixpath.join(parts.path, prefix+suffix)
            ret = parts._replace(path=path) 

            sequence = wget.download(urllib.parse.urlunparse(ret), out=species)
        except :
            print ("")
python pandas • 326 views
ADD COMMENT
2
Entering edit mode

Write a script that deletes files that are older than three months (or use a find -exec rm {}) and run it using a cron job so it will delete old files at defined periods of time.

Strictly speaking, this is not a bioinformatics question and might be marked off topic and deleted.

ADD REPLY
0
Entering edit mode

Thanks a lot, but I could do this in python right?

ADD REPLY
0
Entering edit mode

I can't do it in the same python script? because the species name is entered as input? I don't see how I could connect two scripts in this way sorry

ADD REPLY
0
Entering edit mode

Every time you run this script, you'd like for it to look for files older than 3 months in the same species-named directory and delete them? If that's the requirement, you can definitely do that - google "python find files older than three months" and "python delete files". There should be libraries/modules that will help you do these things.

ADD REPLY
0
Entering edit mode

I would like to add a function in my script that allows me to delete the folder created after 3 months. I don't know if this is possible

ADD REPLY
1
Entering edit mode

It would be unadvisable to bake that logic into this script. You would essentially have to rerun the script as a scheduled cronjob, and build in logic to skip all of the unnecessary other computations that generated those files in the first place - or you'd have to make this script run in the background as a daemon for 3 months checking at regular intervals whether the files have reached the expiry date.

I would make a separate script for this completely and do as Ram said: a script which is executed by cron to tidy the files up once their created date reaches 3 months (assuming no other processes are going to touch those files and modify the timestamps). This script could be done in python, but it would be simpler to use bash, but crucially these are separate scripts.

ADD REPLY
0
Entering edit mode

Okay, I'll look into it. Thank you very much.

ADD REPLY
0
Entering edit mode

Either your script needs to run for the three months, or it needs to create another script that deletes the files and set its execution to be triggered in 3 months. The former is ridiculous, and the latter can be achieved using cron jobs. I recommend not setting auto-delete/self-destruct scripts, things can get unreliable and deletion should never be unsupervised.

ADD REPLY
0
Entering edit mode

Okay, I'll look into it. Thank you very much.

ADD REPLY

Login before adding your answer.

Traffic: 1887 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6