[英]Create a folder (if not exists) on google drive and upload a file to it using Python script
[英]How to create a file using a python script in the C drive of a windows PC?
我的問題是,想象一下我需要制作一個 python 腳本,當他運行腳本時,它將在用戶的 C: 驅動器中創建一個文本文件。
我使用的代碼是,
import os
import getpass
#taking the username
uName = getpass.getuser()
#defining the file path and name
pathPy = "C:/Users/"+uName+"/death.txt"
#creating the .txt file in the said path
file1 = open(pathPy, 'w')
file1.write("Hellow Death")
file1.close()
這適用於我的 PC,因為我是管理員。 但是當我嘗試在 VirtualBox 上運行它時,它給出了一個錯誤“權限被拒絕”。
可以通過從這個腳本中制作一個 .exe 來解決這個問題,它會在開始時請求許可嗎? 如果是這樣,請指導我完成它! 我不是 python 專家,所以如果有人能解釋代碼發生了什么,我將不勝感激。 :)
先感謝您。 <3
更新:我制作了一個 .exe 的 python 腳本,當以管理員身份運行時,它完成了這項工作。 但是如果我只是雙擊它,它就不起作用,因為我不知道如何在嘗試安裝時使腳本需要管理員權限。 如果有人能對此有所了解,那就太棒了! :)
如果有人遇到同樣的問題,我想知道我是否在安裝時讓 exe 需要管理員權限,它可以完成這項工作。 怎么做:
我使用 pyinstaller 從 python 腳本制作 exe。
pyinstaller scriptname.py --onefile --uac-admin
這樣,在安裝時,它會提示輸入管理員權限。 因此,工作! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.