如何读/写文件到未知的用户目录? [英] How do I read/write files to an unknown user directory?

查看:108
本文介绍了如何读/写文件到未知的用户目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用户目录中读取和写入文件(C:\Users\USERNAME\Test Source)但是,我没有找到任何关于如何自动检测名称的资源在上面的例子中,用户USERNAME,或者无论我们知道什么是用户名,我可以让它读取和写入目录。
任何人都可以指向正确的方向或方法,如果它甚至是一个逻辑的请求?我不知道它有多少差异,如果有的话,这个程序是用Python 2.7编写的。

I'm attempting to read and write files from a User Directory, (C:\Users\USERNAME\Test Source) But I've been unsuccessful in finding any resources on how I can auto detect the name of the user, USERNAME in the above example, or anyway that I can have it read and write to the directory without knowledge off what a users name is. Could anyone point me towards the right direction or methods for this, if it's even a logical request? I'm not sure how much difference, if any, it makes but this program is being written in Python 2.7.

推荐答案

你可以在Windows命令行中使用

You can use in windows command line

 echo %username%

  whoami

获取用户当前登录的用户名。
将其存储在变量中,然后将其附加到路径名。

for getting the username of the user who is currently logged in . Store it in a variable and then append it to the path name.

您还可以使用

‘C:\users\%username%\file‘

直接通过 whoami do

l=`whoami`
echo $l

这篇关于如何读/写文件到未知的用户目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆