简体   繁体   English

我的脚本适用于 Windows,但不适用于 Linux-关于文件夹路径

[英]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.

相关问题 Python脚本在Spyder中完美运行(在Windows上) - 在Linux上不起作用 - Python script runs perfectly in Spyder (on Windows) - Doesn't work on Linux Python脚本在Windows上不起作用(但适用于mac) - Python script doesn't work on Windows (but works on mac) 没有完整路径,为什么我安装的(在虚拟环境中的Windows上)python脚本无法运行? - Why doesn't my installed (on Windows in a virtual environment) python script work without a full path? SMTP电子邮件脚本不适用于Mac,但适用于Windows - SMTP Email script doesn't work on Mac but works on Windows 脚本在Linux上不起作用 - Script doesn't work on Linux 为什么此看门狗脚本在我的主文件夹中不起作用? - Why doesn't this watchdog script work in my home folder? python Popen 适用于 linux 但不适用于 windows - python Popen works on linux but doesn't on windows 即时通讯在Linux上,此脚本不会为我带来麻烦,但它适用于Windows 7上的朋友 - im on linux and this script wont wort for me but it works for my friends on windows 7 当我使用“python3 绝对路径”运行 Python 脚本时,它不起作用,但是当我浏览文件夹然后运行时它起作用 - Python script doesn't work when i run it with “python3 absolute path” but work when i navigate on the folder then run stdout不会在Windows命令提示符下刷新,但可以在OSX和Linux上使用 - stdout doesn't flush on Windows command prompt but works on OSX and Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM