首页 > 解决方案 > 如何让我的 Python 脚本在网络文件上工作?

问题描述

我正在编写一个脚本来从源文件中读取创建日期并更改目标文件上的创建日期以匹配它。

它读取本地和 NAS 上存储的文件的创建日期。

它更改本地存储的文件的创建日期。

但是,我无法更改存储在 NAS 上的文件的创建日期。我没有收到错误消息,它只是不会更改创建日期。

关于如何让它发挥作用的任何想法?还是我必须将文件复制到本地文件夹,进行更改,然后将它们复制回来?

创建日期.py

# Get Creation Date of source file
def get_creeation_date(fn):
    return time.ctime(os.path.getctime(fn))

# Convert datetime to integer (for conversion)
def convert_to_integer(dt_time_str):
    dt_time = datetime.strptime(dt_time_str, "%a %b  %d %H:%M:%S %Y")
    return int(datetime.timestamp(dt_time))

# Change the creation date of the destination files based on the source file
# Working with module from this post: >>https://stackoverflow.com/q/47839248/11792868
def changeFileCreationTime(fname, newtime_int):
    wintime = pywintypes.Time(newtime_int)
    winfile = win32file.CreateFile(
        fname,
        win32con.GENERIC_WRITE,
        win32con.FILE_SHARE_READ
        | win32con.FILE_SHARE_WRITE
        | win32con.FILE_SHARE_DELETE,
        None,
        win32con.OPEN_EXISTING,
        win32con.FILE_ATTRIBUTE_NORMAL,
        None,
    )

    win32file.SetFileTime(winfile, wintime, None, None)

    winfile.close()


def main(fn_src, fn_dest):

    changeFileCreationTime(fn_dest, convert_to_integer(get_creeation_date(fn_src)))


if __name__ == "__main__":

    file_src = r"\\path\to\source\file 1.mp4"                               # Located in NAS folder

    file_src_dir, file_src_name_ext = os.path.split(file_src)
    file_src_name, file_src_ext = os.path.splitext(file_src_name_ext)

    file_dest_jpg_1 = re.sub(r"(\s\d$|$)", " 2.jpg", file_src_name, 1)      # file to be created to local folder
    file_dest_jpg_2 = "\\\\path\\to\\source\\" + file_src_name + "2.jpg"    # file to be created to NAS folder
    # Also tried r"\\path\to\source\" + file_src_name + "2.jpg"

    if os.path.exists(file_dest_jpg_1) == False:
        with open(file_dest_jpg_1, "w"):
            pass
    if os.path.exists(file_dest_jpg_2) == False:
        with open(file_dest_jpg_2, "w"):
            pass

    main(file_src, file_dest_jpg_1)                                         # This works
    main(file_src, file_dest_jpg_2)                                         # This does not work

标签: python-3.xwindowsdatetimefile-management

解决方案


推荐阅读