python-os

Environment variable is returning 'None'

Environment variable is returning 'None' Question: I am using windows 11 and the latest version of Pycharm. I try to save environment variable using pycharm termimal by typing ‘export API_KEY=dfdfdg’ and when I type ‘env’ and run then the termianl show all the environment variable including ‘API_KEY’ but in my code, when I wanna fetch …

Total answers: 2

Python program to rename files doesn't work on files on external drives

Python program to rename files doesn't work on files on external drives Question: I wrote this program to rename all files in a directory to title case, and it works fine on files on the system. But when I use it to rename files on an external drive, the program executes successfully without any exceptions …

Total answers: 1

How to find file location using the regular expression ("*" in the path)?

How to find file location using the regular expression ("*" in the path)? Question: Following cp linux command is working fine to find a file "/home/temp/test-1.34.56/sample" to current location Shell command: Working fine cp "/home/temp/test-*/sample" "./" Python code: It not working using os.rename os.rename("/home/temp/test-*/sample", "./") any other options ? Asked By: Lava Sangeetham || Source …

Total answers: 1

Python cannot start a file

Python cannot start a file Question: os.startfile either gives "UnicodeError" or "FileNotFoundError". How to fix this? I also used a "Try Except" in UnicodeError, but still UnicodeError. I used this code: import os os.startfile("C:\Users\Me\AppData\Local\Microsoft\Windows\WinX\Group2\Run.lnk") I expected that Run (Windows + R) should open I got a "FileNotFoundError". I checked the file in location and it …

Total answers: 1

Custom naming of files in Python using pathlib library

Custom naming of files in Python using pathlib library Question: With the os library you can pass a variable to os.path.join as a file name when creating the file. file_name = "file.txt" folder_with_files = os.getcwd() with open(os.path.join(folder_with_files,f"{file_name}.txt"),’w’) as file: file.write("fail") Is there any way to have the same functionality with the pathlib library? The following, …

Total answers: 2

Create folders with file name and rename part of it

Create folders with file name and rename part of it Question: i have some pdfs files that i need to create folders with part of your name and move the pdfs to the folders. cwd = os.getcwd() padrao = ‘_V1_A0V0_T07-54-369-664_S00001.pdf’ for file in glob.glob("*.pdf"): dst = cwd + "\" + file.replace(str(padrao), ”).replace(‘P’, ”) os.mkdir(dst) shutil.move(file, …

Total answers: 1

Batch Rename files with different Prefixes but same file type using python

Batch Rename files with different Prefixes but same file type using python Question: I am trying to rename my files incrementally with a counter starting at 1, which process them in a way depending on their prefix and same file extension. The directory has the following files examples: BS – foo.fxp BS – bar.fxp BS …

Total answers: 2