简体   繁体   English

使用Python处理文件路径中的用户名

[英]Handling usernames in file paths using Python

I'm writing a program that I would like to make a stand alone for my company. 我正在编写一个程序,希望成为我公司的独立设备。 It works perfectly when I run it from the sublime text shell and I have everything set up to go except one issue that I can't seem to solve; 当我从sublime文本外壳运行它时,它可以完美工作,并且除了一个似乎无法解决的问题之外,我已经准备好一切。 file paths that involve usernames. 涉及用户名的文件路径。 Does anyone have any suggestion on how to handle this? 有人对如何处理有任何建议吗?

An example is wb.save(r'C:\\Users******\\Desktop\\Excel.xlsx') 一个示例是wb.save(r'C:\\ Users ****** \\ Desktop \\ Excel.xlsx')

I want to make the ****** part either be automatic or an input box. 我想使******部分成为自动或输入框。

os.getlogin() will do os.getlogin()将

import os
path = os.path.join(r'C:\Users',os.getlogin(),'Desktop','Excel.xlsx')
print(path)

Use os.path.expanduser() with '~' where you want the home directory: 在需要主目录的位置,将os.path.expanduser()与'〜'一起使用:

import os
print(os.path.expanduser('~/Desktop/Excel.xlsx'))

Alternatively use pathlib.Path: 或者使用pathlib.Path:

from pathlib import Path
print(Path.home() / 'Desktop' / 'Excel.xlsx')

Awesome! 真棒! Looks like that worked but it presented another error now when I create it as a stand alone. 看起来可行,但是当我将其单独创建时,它又出现了另一个错误。

Wait originally works when I run it from the shell using this code, where EC is expected conditions: 当我使用以下代码从外壳运行它时,Wait最初可以正常工作:

wait.until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_name('AppBody')))   

Whenever I run it as a stand alone though I get the following error: 每当我独立运行它时,都会出现以下错误:

Traceback (most recent call last):
  File "Stand_Alone_CAS_Automation", line 57, in <module>
NameError: name 'wait' is not defined
[17344] Failed to execute script Stand_Alone_CAS_Automation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM