Finding file in windows path - python

I'm trying to get someone else's Python script to run on my computer and part of the script finds a file in the USERPROFILE. Here is that code:
for w in os.walk(os.getenv('USERPROFILE')):
if 'FilenName' in w[1]:
path = str(w[0]) + r'\FilenName\UsrData\Directory\Data'
However, in the above code, the program tries to search in the following directory:
C:\Users\User\AppData\Roaming\
When, in fact, the program should be looking in
C:\Users\User\AppData\Local\
If I replace the problem code with the following, it works, but I need it to run for all USERPROFILEs, not just mine:
path = r'C:\Users\Bill\AppData\Local\FilenName\UsrData\Directory\Data'
What is the solution to this?
Thanks.

I'm not on a windows machine, so this is a bit tricky, but could you find all user profiles using the env var ALLUSERSPROFILE?
Another option may be to replace "Roaming" with "Local" in the string. It's a bit hacky, but can be done:
for w in os.walk(os.getenv('USERPROFILE')):
if 'FilenName' in w[1]:
path = (str(w[0]) + r'\FilenName\UsrData\Directory\Data').replace('Roaming', 'Local')

Related

Cannot find a solution to [WinError 2] File not found by AudioSegment.from_mp3

Even though there are a handful of threads on this issue, no solutions have helped me, here is the problematic lines of code:
AudioSegment.converter = r'C:/users/user_/appdata/local/packages/pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0/localcache/local-packages/python38/site-packages/ffmpeg.exe'
AudioSegment.ffprobe = r'C:/users/user_/appdata/local/packages/pythonsoftwarefoundation.python.3.8_qbz5n2kfra8p0/localcache/local-packages/python38/site-packages/ffprobe.exe'
final_voice = AudioSegment.from_mp3(file_path) + AudioSegment.silent(duration=silence_duration)
I have tried different methods to solve this issue, such as adding the paths to ffmpeg.exe and ffprobe.exe but nothing changed after that, other solutions do not make much sense as I am not using the modules they have had issues with and I did not do things they have done.
If you have any ideas please share as I have not found how to do the things AudioSegment does with other modules (by the way this issue has come up in every line of code containing AudioSegment.from_mp3(file_path))
Thanks
The solution is quite simple, you have to add ffmpeg.exe, ffprobe.exe and ffplay.exe into your script directory. Download these exe files from the FFMPEG download page and take them from the bin folder
Adding the ffmpeg files was not an option for me so I dug a little deeper. Short answer:
Change the else clause of get_prober_name() in your local ...\site-packages\pydub\utils.py (line 199 in my current version) to return the absolute path of your ffprobe.exe. After that the following code worked for me:
from pydub import AudioSegment
AudioSegment.converter = 'D:/Stuff/Software/ffmpeg/bin/ffmpeg.exe'
AudioSegment.ffprobe = 'D:/Stuff/Software/ffmpeg/bin/ffprobe.exe' # this does nothing!
mp3_fol = "D:/mp3/"
mp3_file = AudioSegment.from_mp3(mp3_fol + "my.mp3")
I'm using Windows with a unzipped version of ffmpeg (so no installation, Path entry or similar). While the converter method seemed to acutally set a value, the ffprobe method didn't do anything.
The subprocess the script is calling simply calls 'ffprobe' as a program. Which of course will not work if this is not a registered program. So the easiest (and hopefully safest) way to circumvent this behaviour is to set the default prober name to the correct full path (as done above).

ValueError: need more than 0 values to unpack (Python 2)

I am trying to replicate another researcher's findings by using the Python file that he added as a supplement to his paper. It is the first time I am diving into Python, so the error might be extremely simple to fix, yet after two days I haven't still. For context, in the Readme file there's the following instruction:
"To run the script, make sure Python2 is installed. Put all files into one folder designated as “cf_dir”.
In the script I get an error at the following lines:
if __name__ == '__main__':
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
os.chdir(cf_dir)
cf = pd.read_csv(cf_file)
cf_phys = pd.read_csv(cf_phys_file)
ValueError: need more than 0 values to unpack
The "cf_file" and "cf_phys_file" are two major components of all files that are in the one folder named "cf_dir". The "cf_phys_file" relates only to two survey question's (Q22 and Q23), and the "cf_file" includes all other questions 1-21. Now it seems that the code is meant to retrieve those two files from the directory? Only for the "cf_phys_file" the columns 1:4 are needed. The current working directory is already set at the right location.
The path where I located "cf_dir" is as follows:
C:\Users\Marc-Marijn Ossel\Documents\RSM\Thesis\Data\Suitable for ML\Data en Artikelen\Per task Suitability for Machine Learning score readme\cf_dir
Alternative option in readme file,
In the readme file there's this option, but also here I cannot understand how to direct the path to the right location:
"Run the following command in an open terminal (substituting for file names
below): python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile
jobDataFile OESfile
This should generate the data and plots as necessary."
When I run that in "Command Prompt", I get the following error, and I am not sure how to set the working directory correctly.
- python: can't open file 'cfProcessor_AEAPnP.py': [Errno 2] No such file or directory
Thanks for the reading, and I hope there's someone who could help me!
Best regards & stay safe out there during Corona!!
Marc
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
means, the python file expects few arguments when called.
In order to run
python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile jobDataFile OESfile
the command prompt should be in that folder.
So, open command prompt and type
cd path_to_the_folder_where_ur_python_file_is_located
Now, you would have reached the path of the python file.
Also, make sure you give full path in double quotes for the arguments.

Python won't create / write to a file

I am new at python and learning the language. The following code should create a file in the running program directory and write to it but it doesn't do this at all in a .py file. If I put the same code in the IDLE shell it returns 17. No errors just doesn't create the file. What am I doing wrong?
with open("st.txt", "w") as f:
f.write("Hi from Python!")
Thanks for the help
Mike
This code is flawless, no problem!
I guess that in your REPL shell, the $PWD environment variable is set for somewhere, so your destination file is in some corner.
No exception thrown indicates that no problem with access authority.
Maybe you can set some absolute path string, such as ~/st.txt
By the way, the successful invoke should return 15 instead of 17, totally count 15 chars.
your code works well, st.txt will be touched at executing path.
other ways, your system account can't write in your execute path.
try in your $HOME path to execute your code, I think, It will work well

Running python cgi script Interpreter results differ to browser

I was having difficulty converting a program I made to a cgi script. I suspected it was to do with os.walk so I made a smaller test script to test this.
(I noticed the single \ before the D in the variable loc and tried changing that to a double \ still no change)
Produces no errors cant tell why it doesn't run the for loop with os.walk in the browser.
I tried adding some data into s and run for loop printing of contents of it and that worked fine, but trying to do it on os.walk I can't seem to get it to work. I can't find anything relating to the issue on google or stackoverflow.
Below is the code:
import cgi,cgitb,os
loc = "C:\\Users\\wen\Desktop\\sample data\\old py stuff\\"
cgitb.enable(display=1,logdir=loc)
s = []
print("Content-type:text/html\r\n\r\n")
print("<html>")
print("<body>")
print("<p>"+loc+"</p>")
for r,ds,fs in os.walk(loc):
print("<p>omgwtf</p>")
for f in fs:
s.append(f)
for i in s:
print("<p>"+i+"</p>")
print("</body>")
print("</html>")
Took a screenshot, the output in interpreter on the left and browser on right
i.imgur.com/136y1Yq.jpg
webserver is running iis7
I'm pretty sure I've solved the problem, I needed to give the folders permissions for 'Authenticated users'.

Read registry value with python problems

I feel like I am taking crazy pills. So for security on an api at work I am using, I have to read 2 things from the registry, that I then pass to suds. The problem is with reading the registry values. No matter what I do, I get "Error2 the system cannot find the file specified". I know that the registry file is there, yet it won't let me read it. I have tried the code below on 2 different 2008 r2 servers. On one windows 7 box, I am able to read the values...but only on one machine. Below is the code, with the actual directory I need changed(to protect anonymity)
from _winreg import *
key = OpenKey(HKEY_LOCAL_MACHINE, r"Software\a\b", 0, KEY_ALL_ACCESS)
devguid = QueryValueEx(key, "DeviceID")
devid = QueryValueEx(key, "DeviceGUID")
devnm = socket.gethostname()
If I change the directory to something other than \a\b, it works fine. I have verified that the permissions on these directories are the exact same as directories I can read from.
Also, I can run the following command from cmd and get the output I need:
reg query HKLM\software\a\b /v DeviceGUID
But when I run it from a python script, it says cannot find file specified.
import os
cmd = "reg query HKEY_LOCAL_MACHINE\software\a\b /v DeviceGUID"
a = os.system(cmd)
print a
Running my script as admin or anything doesn't help. For some reason, python is unable to try and ready registry....
First of all you do need to make sure that your backslashes are suitably escaped, or use raw strings as per the first code sample. I'm going to assume that you've done that.
The most likely explanation is that you use 32 bit Python on a 64 bit system. And so are subject to the registry redirector serving up the 32 bit view of the registry.
Either use 64 bit Python, or specifically open they key with a 64 bit view. Do the latter by specifying the KEY_WOW64_64KEY flag.

Categories