से शुरू होने वाली सभी निर्देशिकाओं/उप-निर्देशिकाओं में सभी नव निर्मित, संशोधित और हटाई गई फ़ाइलों को खोजने के लिए पाइथन कोड मुझे पता है कि निर्देशिका उपखंड में सभी उपनिर्देशिकाओं और फ़ाइलों को कैसे सूचीबद्ध किया जाए। लेकिन मैं रूट निर्देशिका से शुरू होने वाली निर्देशिका पेड़ में सभी निर्देशिकाओं में हटाई गई सभी नई बनाई गई फ़ाइलों को संशोधित करने और संशोधित (यदि संभव हो) सूचीबद्ध करने का तरीका ढूंढ रहा हूं।पायथन कोड/निर्देशिका
उत्तर
आप प्रत्येक फ़ाइल की "mtime" को देखकर बनाया या पिछले आधे घंटे में संशोधित सभी फाइलों को मिल सकता है:
import os
import datetime as dt
now = dt.datetime.now()
ago = now-dt.timedelta(minutes=30)
for root, dirs,files in os.walk('.'):
for fname in files:
path = os.path.join(root, fname)
st = os.stat(path)
mtime = dt.datetime.fromtimestamp(st.st_mtime)
if mtime > ago:
print('%s modified %s'%(path, mtime))
हटाई गई फ़ाइलें की एक सूची उत्पन्न करने के लिए, आप भी करना होगा 30 मिनट पहले फाइलों की एक सूची है।
एक और अधिक मजबूत विकल्प git
की तरह एक संशोधन नियंत्रण प्रणाली का उपयोग करने के लिए है। निर्देशिका में सभी फाइलों का प्रतिबद्ध बनाना स्नैपशॉट बनाना है। फिर आदेश
git status -s
अंतिम प्रतिबद्धता के बाद से परिवर्तित की गई सभी फ़ाइलों को सूचीबद्ध करेगा। यह उन फ़ाइलों को सूचीबद्ध करेगा जो हटा दिए गए हैं।
उपरोक्त कोड को चलाने से, निम्न त्रुटि मिलती है: ट्रेसबैक (सबसे हालिया कॉल अंतिम): फ़ाइल "tsck.py", पंक्ति 13, में? प्रिंट ('{p} संशोधित {m}' प्रारूप (पी = पथ, एम = एमटाइम)) विशेषताएँ त्रुटि: 'str' ऑब्जेक्ट में कोई विशेषता नहीं है 'प्रारूप' – nsh
इतनी धीमी है, हम एक और तरीका ढूंढ सकते हैं, सक्रिय कर सकते हैं नई बनाई गई फ़ाइलों को लॉग करने के लिए सिस्टम, और फिर लॉग फ़ाइलों को पार्स करना। या बेहतर तरीका है नई लॉग प्रविष्टि के लिए एक ट्रिगर जोड़ने के लिए। मदद करें! – pylover
@ एनएसएच: 'str.format' पायथन 2.6 में पेश किया गया था। पिछले संस्करण के लिए, आप '% s'-style स्ट्रिंग स्वरूपण का उपयोग कर सकते हैं। मैं अपनी पोस्ट को संपादित करने के लिए संपादित करूंगा जो मेरा मतलब है। – unutbu
पर एक नजर डालें "आदमी को खोजने के"
एक अस्थायी फ़ाइल बनाने की तुलना करने के
उदाहरण:
find/-type f -newerB tempFile
आदमी का कुछ हिस्सा लगता है
-newerXY reference
Compares the timestamp of the current file with reference. The reference argument is normally the name of a file (and one
of its timestamps is used for the comparison) but it may also be a string describing an absolute time. X and Y are place‐
holders for other letters, and these letters select which time belonging to how reference is used for the comparison.
a The access time of the file reference
B The birth time of the file reference
c The inode status change time of reference
m The modification time of the file reference
t reference is interpreted directly as a time
from tempfile import mkstemp
import shutil
import os
import datetime as dt
import sys
# gets the time frame we are going to look back and builds a placeholder list to passover the info from our mtime to slay
now=dt.datetime.now()
ago=now-dt.timedelta(minutes=480)
passover=[]
# the '.' is the directory we want to look in leave it to '.' if you want to search the directory the file currently resides in
for root,dirs,files in os.walk('.'):
for fname in files:
path=os.path.join(root,fname)
st=os.stat(path)
mtime=dt.datetime.fromtimestamp(st.st_mtime)
if mtime>ago:
passover.append(path)
def slay(file_path, pattern, subst):
#Create temp file
fh, abs_path = mkstemp()
with open(abs_path,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
new_file.write(line.replace(pattern, subst))
old_file.close()
#Remove original file
os.remove(file_path)
#Move new file
try:
shutil.move(abs_path, file_path)
except WindowsError:
pass
#we pass the passover list to the slay command in a for loop in order to do muiltple replaces in those files.
for i in passover:
slay(i,"String1","String2")
मैंने इसे एक डीआईआर में देखने के लिए बनाया और संशोधित फ़ाइलों को अंतिम समय के भीतर चुनें और फिर उन फ़ाइलों में टेक्स्ट को प्रतिस्थापित करें। यह स्क्रिप्ट चारों ओर बिछा नहीं रही थी और मुझे ऊपर दिए गए उत्तर से इसे एक साथ टुकड़ा करना पड़ा था, इसलिए मुझे लगा कि कोई और इसे ढूंढ सकता है। – Powerboy2
कृपया इस जानकारी के साथ अपना जवाब संपादित करें। साथ ही, एक पूर्ण उत्तर को कुछ पंक्तियों का सामना करना चाहिए जो वर्णन करता है कि यह क्या करता है। कृपया निम्नलिखित आलेख पढ़ें: [मैं एक अच्छा उत्तर कैसे लिखूं?] (Http://stackoverflow.com/help/how-to-answer) – Mariano
कृपया निर्दिष्ट करें कि आपके लिए नव निर्मित कौन सा है। आखिरी घंटे के भीतर? आखरी दिन? एक साल से? यदि आप जानते हैं कि निर्देशिका पेड़ कैसे बनाया जाए, तो आप फ़ाइल गुणों तक पहुंचने के लिए 'os.lstat' का उपयोग क्यों नहीं करते? – hochl
अंतिम घंटे के भीतर .... – nsh
फिर 'st = os.lstat (filepath) 'और' st.st_mtime' फ़ील्ड का उपयोग करें और जांचें कि वर्तमान समय में अंतर 1800 से कम है या नहीं। – hochl