我一直在尝试将 Python 脚本(通过 Pyinstaller 的 .PY 和 .EXE)设置为 Windows 服务。我面临的挑战是遇到的大多数问题似乎与我自己的问题无关。
我成功设置了一次虚拟环境,并运行了一个示例服务。但是,在记录我的步骤并尝试重新创建虚拟环境并运行服务时,我遇到了问题。
> python -m venv venv
> venv\scripts\activate.bat
> pip install pypiwin32
Run Code Online (Sandbox Code Playgroud)
然后按照此StackOverflow Question中的说明进行操作。我按照lindsay.stevens.au的回答运行:
> python myvenv\Scripts\pywin32_postinstall.py -install
Run Code Online (Sandbox Code Playgroud)
然后复制venv\Lib\site-packages\win32\pythonservice.exe到venv\Scripts\pythonservice.exe
我一直在 GitHub 上使用 HaroldMils 的这个服务示例作为app.py。
当我运行这个时:
> python app.py install
Installing service PythonExample
Service installed
> python app.py debug
Debugging service PythonExample - press Ctrl+C to stop.
Error 0xC0000005 - Python could find the service class in the module
<Error …Run Code Online (Sandbox Code Playgroud) 为了给予信任,我当前正在使用的代码来自cji的回复,此处。
我试图以递归方式从源文件夹中提取所有文件,并将它们从文件名前五个字符移动到文件夹中0:5
我的代码如下:
import os
import shutil
srcpath = "SOURCE"
srcfiles = os.listdir(srcpath)
destpath = "DESTINATION"
# extract the three letters from filenames and filter out duplicates
destdirs = list(set([filename[0:5] for filename in srcfiles]))
def create(dirname, destpath):
full_path = os.path.join(destpath, dirname)
os.mkdir(full_path)
return full_path
def move(filename, dirpath):
shutil.move(os.path.join(srcpath, filename)
,dirpath)
# create destination directories and store their names along with full paths
targets = [(folder, create(folder, destpath)) for folder in destdirs]
for dirname, full_path in targets: …Run Code Online (Sandbox Code Playgroud)