Hướng dẫn rename images in python - đổi tên hình ảnh trong python

8

Nội dung chính ShowShow

  • Làm thế nào để bạn tự động đổi tên một tệp trong Python?
  • Làm thế nào để bạn đặt tên cho một tệp trong một ngày trong Python?
  • Làm cách nào để đổi tên các tệp trong Python?
  • Làm thế nào để bạn hẹn giờ trong Python?

Nội dung chính

  • Làm thế nào để bạn tự động đổi tên một tệp trong Python?
  • Làm thế nào để bạn đặt tên cho một tệp trong một ngày trong Python?
  • Làm cách nào để đổi tên các tệp trong Python?
  • Làm thế nào để bạn hẹn giờ trong Python?

Nội dung chính
Learn more.

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.Learn more.Learn more.

Tôi đang sử dụng Python v2.x và đang tự hỏi làm thế nào tôi có thể đổi tên một tệp văn bản đã biết, ví dụ của tôi nói "text.txt", để bao gồm ngày và thời gian hiện tại.

Mọi sự trợ giúp sẽ rất được trân trọng.Mar 7, 2011 at 10:03

Hướng dẫn rename images in python - đổi tên hình ảnh trong python

Đã hỏi ngày 7 tháng 3 năm 2011 lúc 10:03Mar 7, 2011 at 10:03Mar 7, 2011 at 10:03

import os
import date

timestamp = datetime.datetime.now()
t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second

split_filename = filename.split('.')
os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))
1. Lưu ý rằng bạn phải
import os
import date

timestamp = datetime.datetime.now()
t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second

split_filename = filename.split('.')
os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))
2
import os
import date

timestamp = datetime.datetime.now()
t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second

split_filename = filename.split('.')
os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))
3 và
import os
import date

timestamp = datetime.datetime.now()
t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second

split_filename = filename.split('.')
os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))
4.

Hãy xem qua đây để biết những thứ thời gian và ở đây để đổi tên các tập tin.Mar 7, 2011 at 10:10

Đã trả lời ngày 7 tháng 3 năm 2011 lúc 10:10Mar 7, 2011 at 10:10Mar 7, 2011 at 10:10phimuemue

Phimuemuephimuemuephimuemuephimuemue9 gold badges79 silver badges112 bronze badges

0

33.3k9 Huy hiệu vàng79 Huy hiệu bạc112 Huy hiệu đồng9 gold badges79 silver badges112 bronze badges9 gold badges79 silver badges112 bronze badges

import datetime
dt = str(datetime.datetime.now())

Để sử dụng DateTime hiện tại:

import os
newname = 'file_'+dt+'.txt'
os.rename('text.txt', newname)

Sau đó để đổi tên tệp:Mar 7, 2011 at 10:09

Đã trả lời ngày 7 tháng 3 năm 2011 lúc 10:09Mar 7, 2011 at 10:09Mar 7, 2011 at 10:09ikostia

Ikostiaikostiaikostiaikostia6 gold badges30 silver badges39 bronze badges

2

7.0276 Huy hiệu vàng30 Huy hiệu bạc39 Huy hiệu Đồng6 gold badges30 silver badges39 bronze badges6 gold badges30 silver badges39 bronze badges

import os
import datetime

src = '/home/thewoo/text.txt'
dst = '/home/thewoo/%s-text.txt' % datetime.datetime.now()
os.rename(src, dst)

Os.rename (SRC, DST)

Sửa đổi

Hãy xem qua đây để biết những thứ thời gian và ở đây để đổi tên các tập tin.Mar 7, 2011 at 10:10

Đã trả lời ngày 7 tháng 3 năm 2011 lúc 10:10Mar 7, 2011 at 10:10Mar 7, 2011 at 10:10DisplacedAussie

Phimuemuephimuemuephimuemuephimuemue1 gold badge26 silver badges21 bronze badges

import os
import date

timestamp = datetime.datetime.now()
t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second

split_filename = filename.split('.')
os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))

33.3k9 Huy hiệu vàng79 Huy hiệu bạc112 Huy hiệu đồng9 gold badges79 silver badges112 bronze badges9 gold badges79 silver badges112 bronze badgesMar 7, 2011 at 12:22

Để sử dụng DateTime hiện tại:gladysbixly

Sau đó để đổi tên tệp:17 silver badges15 bronze badges

0

Đã trả lời ngày 7 tháng 3 năm 2011 lúc 10:09Mar 7, 2011 at 10:09Mar 7, 2011 at 10:09

Ikostiaikostiaikostiaikostia

7.0276 Huy hiệu vàng30 Huy hiệu bạc39 Huy hiệu Đồng6 gold badges30 silver badges39 bronze badges6 gold badges30 silver badges39 bronze badges

  • Os.rename (SRC, DST)
  • Sửa đổi
  • Đã trả lời ngày 7 tháng 3 năm 2011 lúc 10:09Mar 7, 2011 at 10:09Mar 7, 2011 at 10:09

    Ikostiaikostiaikostiaikostia

    7.0276 Huy hiệu vàng30 Huy hiệu bạc39 Huy hiệu Đồng6 gold badges30 silver badges39 bronze badges6 gold badges30 silver badges39 bronze badges

    Os.rename (SRC, DST)

    Sửa đổi

    import os
    import date
    
    timestamp = datetime.datetime.now()
    t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second
    
    split_filename = filename.split('.')
    os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))
    
    5 và thời gian diễn ra ngày theo yêu cầu.ISO 8601 timestamp. Which would be used to name the file.

    PhimuemuephimuemuephimuemueDisplacedAussie

    • 33.3k9 Huy hiệu vàng79 Huy hiệu bạc112 Huy hiệu đồng9 gold badges79 silver badges112 bronze badges1 gold badge26 silver badges21 bronze badgesos.path.getctime() method in Python is used to get system’s ctime of the specified path. Here ctime refers to the last metadata change for specified path in UNIX while in Windows, it refers to path creation time.
    • Để sử dụng DateTime hiện tại:Mar 7, 2011 at 12:22It is used to convert the string object to time object.
    • Sau đó để đổi tên tệp:gladysbixly time.strftime(format[, t]) function convert a tuprl or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument.
      If t is not provided, the current time as returned by localtime() is used. The format must be a string.
    • Đã trả lời ngày 7 tháng 3 năm 2011 lúc 10:09Mar 7, 2011 at 10:0917 silver badges15 bronze badges os.rename() method in Python is used to rename a file or directory.
      This method renames a source file/ directory to specified destination file/directory.

    Ikostiaikostiaikostia

    Python3

    7.0276 Huy hiệu vàng30 Huy hiệu bạc39 Huy hiệu Đồng6 gold badges30 silver badges39 bronze badges

    Sửa đổi

    import os
    import date
    
    timestamp = datetime.datetime.now()
    t = timestamp.year, timestamp.month, timestamp.day, timestamp.hour, timestamp.minute, timestamp.second
    
    split_filename = filename.split('.')
    os.rename(filename, split_filename[:-1] + '_' + '-'.join(t))
    
    5 và thời gian diễn ra ngày theo yêu cầu.

    PhimuemuephimuemueDisplacedAussie

    PhimuemuephimuemueDisplacedAussieISO 8601 timestamp. Which would be used to name the file.

    33.3k9 Huy hiệu vàng79 Huy hiệu bạc112 Huy hiệu đồng1 gold badge26 silver badges21 bronze badges

    • Để sử dụng DateTime hiện tại:Mar 7, 2011 at 12:22os.path.getctime() method in Python is used to get system’s ctime of the specified path. Here ctime refers to the last metadata change for specified path in UNIX while in Windows, it refers to path creation time.form_t = form_t.replace(“:”, “꞉”) statement, as it is only required in windows as the OS doesn’t allow a colon as a filename. For use in other OS, the last statement (os.rename()) should also be modified accordingly.
    • Sau đó để đổi tên tệp:gladysbixlyIt is used to convert the string object to time object.“%Y-%m-%d %H:%M:%S” is a format specifier. This is used to guide the output of strftime(). This format specifier could be changed in order to the syntax of other timestamp standards.
    • Os.Path.Split (f_path) [0] Trong câu lệnh cuối cùng là để đưa đường dẫn đến gốc (thư mục cha) của tệp.in the last statement is for getting the path to the root (parent directory) of the file.in the last statement is for getting the path to the root (parent directory) of the file.in the last statement is for getting the path to the root (parent directory) of the file.
    • os.path.splitext (f_path) [1] là để thêm tiện ích mở rộng tệp (nếu có) của tệp gốc vào dấu thời gianis for adding the file extension (if any) of the original file to the timestampis for adding the file extension (if any) of the original file to the timestampis for adding the file extension (if any) of the original file to the timestamp

    Làm thế nào để bạn tự động đổi tên một tệp trong Python?

    Hệ điều hành Python.Đổi tên () Phương thức thay đổi tên của một tệp.hệ điều hành.Đổi tên () chấp nhận hai đối số: đường dẫn của tệp cũ và đường dẫn của tệp mới.

    Làm thế nào để bạn đặt tên cho một tệp trong một ngày trong Python?

    Đối với điều này, chúng tôi sẽ sử dụng mô -đun DateTime.Sử dụng các khái niệm xử lý tệp trong Python.use the DateTime module.First, import the module and then get the current time with datetime.now() object.Now convert it into a string and then create a file with the file object like a regular file is created using file handling concepts in python.use the DateTime module.First, import the module and then get the current time with datetime.now() object.Now convert it into a string and then create a file with the file object like a regular file is created using file handling concepts in python.use the DateTime module. First, import the module and then get the current time with datetime. now() object. Now convert it into a string and then create a file with the file object like a regular file is created using file handling concepts in python.

    Làm cách nào để đổi tên các tệp trong Python?

    Để đổi tên các tệp theo Python, hãy sử dụng phương thức đổi tên () của mô -đun HĐH.Các tham số của phương thức đổi tên () là địa chỉ nguồn (tên cũ) và địa chỉ đích (tên mới).use the rename() method of the os module. The parameters of the rename() method are the source address (old name) and the destination address (new name).use the rename() method of the os module. The parameters of the rename() method are the source address (old name) and the destination address (new name).use the rename() method of the os module. The parameters of the rename() method are the source address (old name) and the destination address (new name).

    Làm thế nào để bạn hẹn giờ trong Python?

    Python cung cấp các thư viện khác nhau để làm việc với dữ liệu dấu thời gian .....

    Đầu tiên, nhập cả thời gian và mô -đun lịch ..

    Tiếp theo, hãy lấy thời gian GMT bằng thời gian của mô -đun thời gian.phương thức gmtime () ..

    Cuối cùng, chuyển nó sang phương thức sử dụng lịch.timegm () để có dấu thời gian ..