python-3.x - 如何让我的 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
解决方案
推荐阅读
- android - 如何在 AppCenter 上使用上传的发布密钥库签署 Android 发布版本?
- snowflake-cloud-data-platform - 使用存储过程插入表?
- java - 如何在android Firestore的文档字段中保存文档ID?
- uefi - GRUB 黑屏
- r - 尝试从包含 70 个 pdf 文件的目录中的每个 pdf 中提取页面子集
- android - Android在MaterialDialog中对齐中心或右对齐标题和消息
- javascript - 闪电输入字段的值未定义,即使它具有值
- c# - 如何使用 ToList() 打印多个列表项?
- jupyter-lab - 如何启用 vim 模式以在 jupyter-lab 中编辑笔记本?
- postgresql - 我可以将 Amazon RDS PostgreSQL 与 Active Directory AD 或 AD 连接器连接吗?