[英]My script works on Windows but doesn't work on Linux-About Folder Path
I wrote a script.It is getting a file modify time from a Windows Server.我写了一个脚本。它正在从 Windows 服务器获取文件修改时间。 It is working on Windows but not on Linux.它适用于 Windows,但不适用于 Linux。 It means there is a file on Windows Server and I want to get this file's modify time from Linux.这意味着 Windows Server 上有一个文件,我想从 Linux 获取该文件的修改时间。 I tried backslashes, double slashes and pathlib library but not working.我尝试了反斜杠、双斜杠和 pathlib 库,但没有工作。 Here is my Linux output.这是我的 Linux 输出。
[root@server03]# python3 serveropen2.py
Traceback (most recent call last):
File "serveropen2.py", line 5, in <module>
modify = dt.datetime.fromtimestamp(os.path.getmtime(server))
File "/usr/lib64/python3.6/genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [Errno 2] No such file or directory: '\\Server1\tools\folder\capture.JPG'
Here is my codes这是我的代码
import os
import datetime as dt
server=r'\\Server1\tools\folder\capture.JPG'
modify = dt.datetime.fromtimestamp(os.path.getmtime(server))
if dt.datetime.now() - modify > dt.timedelta(minutes=5):
print("old")
else:
print("new")
You need to use the currect path separator.您需要使用当前路径分隔符。 Under windows \\ is the separator, under linux / is the separator. windows下\\是分隔符,linux下/是分隔符。
So instead of所以代替
\\\\Server1\\tools\\folder\\capture.JPG'
your script has to use你的脚本必须使用
/Server1/tools/folder/capture.JPG'
By using os.path.join you can fix this:通过使用os.path.join你可以解决这个问题:
import os
server = os.path.join('Server1', 'tools', 'folder', 'capture.JPG')
...
And you need to know where the file is located in your filesystem on linux.并且您需要知道文件在 linux 上的文件系统中的位置。 If it is located somewhere in your home-directory you can do something like:如果它位于您的主目录中的某个位置,您可以执行以下操作:
import os
my_server_dir = os.path.join('~', 'server_home')
server = os.path.join('Server1', 'tools', 'folder', 'capture.JPG')
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.