ฉันต้องการรับไฟล์ล่าสุดของโฟลเดอร์โดยใช้ python ขณะใช้รหัส:
max(files, key = os.path.getctime)
ฉันได้รับข้อผิดพลาดด้านล่าง:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'a'
ฉันต้องการรับไฟล์ล่าสุดของโฟลเดอร์โดยใช้ python ขณะใช้รหัส:
max(files, key = os.path.getctime)
ฉันได้รับข้อผิดพลาดด้านล่าง:
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'a'
คำตอบ:
สิ่งที่กำหนดให้กับfiles
ตัวแปรนั้นไม่ถูกต้อง ใช้รหัสต่อไปนี้
import glob
import os
list_of_files = glob.glob('/path/to/folder/*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)
print latest_file
if os.path.isdir(latest_file):
pathlib.Path
วัตถุมากกว่าสตริงและ os.path ด้วย pathlib.Path ทำให้คำตอบของคุณกลายเป็น: list_of_paths = folder_path.glob('*'); latest_path = max(list_of_paths, key=lambda p: p.stat().st_ctime)
os.path.getctime
เป็นกุญแจได้แม้กับPath
วัตถุ
max(files, key = os.path.getctime)
รหัสค่อนข้างไม่สมบูรณ์ คืออะไรfiles
? อาจเป็นรายชื่อไฟล์ที่ออกมาจากos.listdir()
.
แต่รายการนี้แสดงเฉพาะส่วนของชื่อไฟล์ (หรือที่เรียกว่า "basenames") เนื่องจากพา ธ เป็นเรื่องปกติ ในการใช้งานอย่างถูกต้องคุณต้องรวมเข้ากับเส้นทางที่นำไปสู่ (และใช้เพื่อให้ได้มา)
เช่น (ยังไม่ทดลอง):
def newest(path):
files = os.listdir(path)
paths = [os.path.join(path, basename) for basename in files]
return max(paths, key=os.path.getctime)
if basename.endswith('.csv')
ลงในความเข้าใจรายการ
ฉันขอแนะนำให้ใช้glob.iglob()
แทนglob.glob()
เนื่องจากมีประสิทธิภาพมากกว่า
glob.iglob () ส่งคืนตัววนซ้ำซึ่งให้ค่าเดียวกับ glob () โดยไม่ต้องจัดเก็บทั้งหมดพร้อมกัน
ซึ่งหมายความว่าglob.iglob()
จะมีประสิทธิภาพมากขึ้น
ส่วนใหญ่ฉันใช้โค้ดด้านล่างเพื่อค้นหาไฟล์ล่าสุดที่ตรงกับรูปแบบของฉัน:
LatestFile = max(glob.iglob(fileNamePattern),key=os.path.getctime)
หมายเหตุ: มีmax
ฟังก์ชันที่แตกต่างกันในกรณีที่พบไฟล์ล่าสุดเราจะใช้ตัวแปรด้านล่าง:
max(iterable, *[, key, default])
ซึ่งจำเป็นต้องทำซ้ำได้ดังนั้นพารามิเตอร์แรกของคุณควรทำซ้ำได้ ในกรณีที่พบจำนวนสูงสุดเราสามารถใช้ beow variant:max (num1, num2, num3, *args[, key])
max()
เรียงลำดับนี้ ในกรณีของฉันฉันใช้ชื่ออื่นkey=os.path.basename
เนื่องจากชื่อไฟล์มีการประทับเวลาอยู่ในนั้น
พยายามจัดเรียงรายการตามเวลาสร้าง ตัวอย่างด้านล่างจัดเรียงไฟล์ในโฟลเดอร์และรับองค์ประกอบแรกซึ่งเป็นข้อมูลล่าสุด
import glob
import os
files_path = os.path.join(folder, '*')
files = sorted(
glob.iglob(files_path), key=os.path.getctime, reverse=True)
print files[0]
ฉันไม่มีชื่อเสียงในการแสดงความคิดเห็น แต่ ctime จากการตอบกลับของ Marlon Abeykoons ไม่ได้ให้ผลลัพธ์ที่ถูกต้องสำหรับฉัน การใช้ mtime เป็นเคล็ดลับ (คีย์ = os.path.get mเวลา))
import glob
import os
list_of_files = glob.glob('/path/to/folder/*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getmtime)
print latest_file
ฉันพบสองคำตอบสำหรับปัญหานั้น:
python os.path.getctime max ไม่ส่งคืน ความแตกต่างล่าสุดระหว่าง python - getmtime () และ getctime () ในระบบยูนิกซ์
(แก้ไขเพื่อปรับปรุงคำตอบ)
ขั้นแรกกำหนดฟังก์ชัน get_latest_file
def get_latest_file(path, *paths):
fullpath = os.path.join(path, paths)
...
get_latest_file('example', 'files','randomtext011.*.txt')
คุณสามารถใช้ docstring ได้ด้วย!
def get_latest_file(path, *paths):
"""Returns the name of the latest (most recent) file
of the joined path(s)"""
fullpath = os.path.join(path, *paths)
หากคุณใช้ Python 3คุณสามารถใช้iglobแทนได้
กรอกรหัสเพื่อส่งคืนชื่อไฟล์ล่าสุด:
def get_latest_file(path, *paths):
"""Returns the name of the latest (most recent) file
of the joined path(s)"""
fullpath = os.path.join(path, *paths)
files = glob.glob(fullpath) # You may use iglob in Python3
if not files: # I prefer using the negation
return None # because it behaves like a shortcut
latest_file = max(files, key=os.path.getctime)
_, filename = os.path.split(latest_file)
return filename
JuniperAccessLog-standalone-FCL_VPN
ส่วนนี้มาจากไหน?
ฉันได้ลองใช้คำแนะนำข้างต้นและโปรแกรมของฉันขัดข้องกว่าที่ฉันจะพบว่ามีการใช้ไฟล์ที่ฉันพยายามระบุและเมื่อพยายามใช้ 'os.path.getctime' มันก็ล้มเหลว สิ่งที่ได้ผลสำหรับฉันในที่สุดคือ:
files_before = glob.glob(os.path.join(my_path,'*'))
**code where new file is created**
new_file = set(files_before).symmetric_difference(set(glob.glob(os.path.join(my_path,'*'))))
รหัสนี้ได้รับวัตถุที่ผิดปกติระหว่างชุดไฟล์สองชุดซึ่งไม่ใช่รายการที่หรูหราที่สุดและหากมีการสร้างไฟล์หลายไฟล์ในเวลาเดียวกันมันอาจจะไม่เสถียร
วิธีที่เร็วกว่ามากบน windows (0.05s) เรียก bat script ที่ทำสิ่งนี้:
get_latest.bat
@echo off
for /f %%i in ('dir \\directory\in\question /b/a-d/od/t:c') do set LAST=%%i
%LAST%
ที่ไหน \\directory\in\question
เป็นไดเรกทอรีที่คุณต้องการที่จะตรวจสอบ
get_latest.py
from subprocess import Popen, PIPE
p = Popen("get_latest.bat", shell=True, stdout=PIPE,)
stdout, stderr = p.communicate()
print(stdout, stderr)
หากพบไฟล์stdout
คือพา ธ และstderr
ไม่มี
ใช้stdout.decode("utf-8").rstrip()
เพื่อรับการแสดงสตริงที่ใช้งานได้ของชื่อไฟล์
ls -Art | tail -n 1
รู้สึกฟรีเพื่อเพิ่มสคริปต์ที่คล้ายกันสำหรับลินุกซ์อาจจะขึ้นอยู่กับ โปรดประเมินประสิทธิภาพของโซลูชันก่อนทำการอ้างสิทธิ์
ฉันใช้สิ่งนี้ใน Python 3 รวมถึงการจับคู่รูปแบบกับชื่อไฟล์
from pathlib import Path
def latest_file(path: Path, pattern: str = "*"):
files = path.glob(pattern)
return max(files, key=lambda x: x.stat().st_ctime)