Python文件创建日期&重命名 - 请求批评 [英] Python File Creation Date & Rename - Request for Critique

查看:153
本文介绍了Python文件创建日期&重命名 - 请求批评的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:当我拍摄一个物体时,我从多个角度拍摄多个图像。乘以我射击的对象数量,我可以生成大量的图像。问题:相机会生成标识为DSCN100001,DSCN100002等的图像。隐藏的。

我编写了一个脚本,提示目录规范(Windows)以及一个前缀,脚本读取文件的创建日期和时间,并相应地重命名文件,前缀将被添加到文件名的前面,因此DSCN100002.jpg可以变成FatMonkey 20110721 17 :51:02时间的细节对我来说是很重要的。



剧本如下:请告诉我Pythonic是否写得不好当然,是否有一个更清洁 - 更有效的方式来做到这一点。谢谢。

  import os 
输入日期时间
target = raw_input('输入完整目录路径')
prefix = raw_input('输入前缀:')
os.chdir(目标)
allfiles = os .listdir(目标)
在所有文件中的文件名:
t = os.path.getmtime(filenam e)
v = datetime.datetime.fromtimestamp(t)
x = v.strftime('%Y%m%d-%H%M%S')
os.rename(filename,前缀+ x +。jpg)


解决方案

你在做Pythonic。你可以跳过 os.chdir(target),然后执行<$
$ b c $ c> os.path.join(target,filename)在循环中。

你可以做 strftime ('{0} - %Y-%m-%d-%H:%M:%S.jpg'.format(prefix))来避免字符串连接。这是我推荐的唯一一个。

您可以重复使用像 temp_date 而不是 t v x 。这可以。



您可以跳过存储临时变量,只是:

  for os.listdir(target)中的文件名:
os.rename(filename,datetime.fromtimestamp(
os.path.getmtime(filename))。strftime(
'{ 0} - %Y-%m-%d-%H:%M:%S.jpeg'.format(prefix)))

通过使用 os.walk(),可以将函数推广到递归目录。

您可以检测文件的文件扩展名,这样不仅适用于 .jpeg p>

您可以确保只重命名格式为 DSCN1 #####。jpeg

$的文件b $ b

Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied by the number of objects I "shoot", I can generate a large number of images. Problem: Camera generates images identified as, 'DSCN100001', 'DSCN100002", etc. Cryptic.

I put together a script that will prompt for directory specification (Windows), as well as a "Prefix". The script reads the file's creation date and time, and rename the file accordingly. The prefix will be added to the front of the file name. So, 'DSCN100002.jpg' can become "FatMonkey 20110721 17:51:02". The time detail is important to me for chronology.

The script follows. Please tell me whether it is Pythonic, whether or not it is poorly written and, of course, whether there is a cleaner - more efficient way of doing this. Thank you.

   import os
   import datetime
   target = raw_input('Enter full directory path: ')
   prefix = raw_input('Enter prefix: ')
   os.chdir(target)
   allfiles = os.listdir(target)
   for filename in allfiles:
        t = os.path.getmtime(filename)
        v = datetime.datetime.fromtimestamp(t)
        x = v.strftime('%Y%m%d-%H%M%S')
        os.rename(filename, prefix + x +".jpg")

解决方案

The way you're doing it looks Pythonic. A few alternatives (not necessarily suggestions):

You could skip os.chdir(target) and do os.path.join(target, filename) in the loop.

You could do strftime('{0}-%Y-%m-%d-%H:%M:%S.jpg'.format(prefix)) to avoid string concatenation. This is the only one I'd reccomend.

You could reuse a variable name like temp_date instead of t, v, and x. This would be OK.

You could skip storing temporary variables and just do:

for filename in os.listdir(target):
    os.rename(filename, datetime.fromtimestamp(
                         os.path.getmtime(filename)).strftime(
                          '{0}-%Y-%m-%d-%H:%M:%S.jpeg'.format(prefix)))

You could generalize your function to work for recursive directories by using os.walk().

You could detect the file extension of files so it would be correct not just for .jpegs.

You could make sure you only renamed files of the form DSCN1#####.jpeg

这篇关于Python文件创建日期&amp;重命名 - 请求批评的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆