I have a particular folder(Test) in my D drive. Every week a new folder gets created inside test like 1234,4231,etc. I want to write a python script which can print the folder name recently created. I was able to do upto this.
我的D驱动器中有一个特定的文件夹(Test)。每周都会在1234,4231之类的测试中创建一个新文件夹。我想编写一个python脚本,可以打印最近创建的文件夹名称。我能够做到这一点。
import os
for file in os.listdir("D:\Test1"):
if file.endswith(".txt"):
#print(os.path.join("/mydir", file))
s = os.path.join("/mydir", file)
2 个解决方案
#1
0
This will give you the names of all directories/folders along with their creation date from recent to old:
这将为您提供所有目录/文件夹的名称及其从最近到旧的创建日期:
import os, time
DIR = "D:\Test1"
t = {}
for dirName in os.listdir(DIR):
if os.path.isdir(os.path.join(DIR, dirName)):
t[str( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(os.path.join(DIR, dirName)))))] = dirName
for key in sorted(t.keys(), reverse=True):
print("%s : %s" % (key, t[key]))
Here
os.listdir(DIR)
gives the name of all the files and directories present in DIR directory.
给出DIR目录中存在的所有文件和目录的名称。
os.path.isdir(SOME_VAR)
checks whether SOME_VAR is a directory or not.
检查SOME_VAR是否是目录。
os.path.join(DIR, dirName)
used to get the absolute path of the directory whose name is stored in dirName like if dirName is 123 the absolute path is D:\Test1\1234
用于获取名称存储在dirName中的目录的绝对路径,如果dirName为123,则绝对路径为D:\ Test1 \ 1234
Output :
2017-12-29 12:41:16 : Python
2017-10-30 11:30:55 : gccPrgms
2017-09-27 15:25:39 : Projects
2017-09-06 11:26:23 : shellscript
2017-06-22 08:59:19 : shashank
#2
0
This will work. This gives you latest folder name
这会奏效。这将为您提供最新的文件夹名称
import os
directory = 'D:\Test1'
s=max([os.path.join(directory,d) for d in os.listdir(directory)], key=os.path.getmtime)
split_str=s.rsplit('\\',1)
final_string=split_str[1]
print final_string
#1
0
This will give you the names of all directories/folders along with their creation date from recent to old:
这将为您提供所有目录/文件夹的名称及其从最近到旧的创建日期:
import os, time
DIR = "D:\Test1"
t = {}
for dirName in os.listdir(DIR):
if os.path.isdir(os.path.join(DIR, dirName)):
t[str( time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(os.path.getctime(os.path.join(DIR, dirName)))))] = dirName
for key in sorted(t.keys(), reverse=True):
print("%s : %s" % (key, t[key]))
Here
os.listdir(DIR)
gives the name of all the files and directories present in DIR directory.
给出DIR目录中存在的所有文件和目录的名称。
os.path.isdir(SOME_VAR)
checks whether SOME_VAR is a directory or not.
检查SOME_VAR是否是目录。
os.path.join(DIR, dirName)
used to get the absolute path of the directory whose name is stored in dirName like if dirName is 123 the absolute path is D:\Test1\1234
用于获取名称存储在dirName中的目录的绝对路径,如果dirName为123,则绝对路径为D:\ Test1 \ 1234
Output :
2017-12-29 12:41:16 : Python
2017-10-30 11:30:55 : gccPrgms
2017-09-27 15:25:39 : Projects
2017-09-06 11:26:23 : shellscript
2017-06-22 08:59:19 : shashank
#2
0
This will work. This gives you latest folder name
这会奏效。这将为您提供最新的文件夹名称
import os
directory = 'D:\Test1'
s=max([os.path.join(directory,d) for d in os.listdir(directory)], key=os.path.getmtime)
split_str=s.rsplit('\\',1)
final_string=split_str[1]
print final_string