-3

I want to delete all the files older than one day except the hidden files

I have tried the below code but it seems it also tries to delete hidden files, how can I modify it so that it deletes all the files but not hidden files?

import os
import time

current_time = time.time()

for f in os.listdir():
    creation_time = os.path.getctime(f)
    if (current_time - creation_time) // (24 * 3600) >= 1:
        os.unlink(f)
        print('{} removed'.format(f))

If it was linux, I could have done,

if not f.startswith('.'):

I have gone through the links: https://bitbucket.org/aafshar/pida-main/src/tip/pida/services/filemanager/filemanager.py

I do not probably understand it. A more simple code would be appreciated.

kajalp
  • 51
  • 5
  • 1
    I'm voting to close this question as off-topic because SO is not a code-writing service – DeepSpace Jul 15 '18 at 16:50
  • I did not get you? How is it off topic? @DeepSpace – kajalp Jul 15 '18 at 16:54
  • See [ask]...... – DeepSpace Jul 15 '18 at 16:55
  • Can u suggest how i can ask it differently? It is my first question – kajalp Jul 15 '18 at 16:58
  • 1
    For starters, show your attempt – DeepSpace Jul 15 '18 at 17:00
  • 3
    Don't ask "I want to do X, please give me teh codez"; ask "I want to do X. I tried Y. My code does this part correctly, but this other part doesn't work because Z. I did research on the topic and found these relevant resources (A, B), but I still can't figure out the solution." In other words, show us that you've made an attempt to solve the problem. This problem consists of 4 smaller problems: Listing all files in a folder, checking if the file is older than a day, checking if it's a hidden file, and deleting it. Surely you can solve one or two of those problems without our help? – Aran-Fey Jul 15 '18 at 17:06
  • I have edited it now – kajalp Jul 15 '18 at 17:09
  • See [Cross platform hidden file detection](//stackoverflow.com/q/284115) to find out how to check if a file is hidden, and [How do I list all files of a directory?](//stackoverflow.com/q/3207219) or [Browse files and subfolders in Python](//stackoverflow.com/q/5817209) to find out how to iterate over files in subfolders. – Aran-Fey Jul 15 '18 at 17:13
  • Hi, i have gone through those links but i was not able to implement it, after spending 1 and half day i thought of asking it in SO. Could you please help me out? – kajalp Jul 15 '18 at 17:25
  • Can u remove the downvotes? – kajalp Jul 15 '18 at 17:33
  • wait for it, i am working on the code – Sriram Arvind Lakshmanakumar Jul 15 '18 at 17:43

1 Answers1

0

This code is for windows file system, i didnt check the unlink part(which is correct, my assumption ): code explain in the code comments. btw 24 * 3600 == 86400 ( 24 hours in seconds)

import os
import time

import win32file
import win32con

from datetime import datetime

#convert float to datetime obj, current time
current_time = datetime.strptime(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),'%Y-%m-%d %H:%M:%S')

for f in os.listdir():
    #file's time to datetime object
    creation_time = datetime.strptime(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(f))),'%Y-%m-%d %H:%M:%S')
    #subtract both the time -->timedelta object
    z= current_time - creation_time

    #get windows attribute for the file
    file_flag = win32file.GetFileAttributesW(f)
    #check the attribute value for files in windows, for hidden files the attribute should be '2'
    is_hidden = file_flag & win32con.FILE_ATTRIBUTE_HIDDEN

    if ((z.seconds >= 86400) and (is_hidden != 2)):  
        os.unlink(f)
        print('{} removed'.format(f))

windows file system code reference : link