Python not interpreting wildcard * properly? - python

For some reason, python doesn't seem to recognize my wildcard command using *.r.
A MWE:
filepath = "C:/outputs"
os.remove(f'{filepath}/*.r')
Python returns the following error:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:/outputs/*.r'
I know there are alternatives to using the "*.r" method, such as using a "for file in os.listdir(...): if file.endswith('.r'): os.remove(file)" etc. But the "os.remove('./*.r')" method works in my other python files, so this is just driving me insane. What might be going wrong?

Related

Problem with path when trying to write a file with a defined name given before by the code in Python

I'm having issues when I try to write a file in Python giving it a certain name and not only pathing it. Here is what I'm trying to do:
page_title=page.find('title')
raw_data_path ='output/'+page_title+'_raw.txt'
print (page_title)
with open(raw_data_path, 'w') as file:
file.write ()
When running this code, I receive the error [Errno 22] Invalid argument: 'output/myfile_raw.txt'
If instead of raw_data_path ='output/'+page_title+'_raw.txt' I put, for example, raw_data_path ='output/'+'_raw.txt' the code works well, so for some reason I can't combine the path with the name I'm trying to give the file.
I've searched that error and I see that it is a routing error, so it might be happening because when I want to add the page_title something happens with the path, but I can't see which is the mistake because it should be working.
Can someone give me some help with this issue?

Parser takes only first character from the filename and says "File not found"

I know it's very difficult to understand my issue, but I'll try my best to explain.
So I've cloned a repository from Github and working on it.
When I run the program without any arguments, it works fine
python main.py --audio works fine.
But when I try to pass my own file, it shows an error.
python main.py --audio myaudio.wav shows an error saying "FileNotFoundError: [Errno 2] No such file or directory: '<File Path/m'"
Notice how it only takes the first character from my argument?
In the code, for default mode, it's something like:
(args.py)parser.add_argument('--audio', default=['default_audio.wav'], type=list) and it works fine.
So naturally, I tried to add '[]' to my file name
python main.py --audio [myaudio.wav] Shows an error too which says "FileNotFoundError: [Errno 2] No such file or directory: '<File Path/['"
See, here it took only the first character of my provided argument i.e. '['
And the file IS there. No spelling mistakes in file name either.
Any help would be very much appreciated.

can't get a file size in python

I am a python newbie and learning from "automate boring stuff" book, so it says in the book the I can use os.path.getsize(path) to get a file size but when I run this code it gives an error, can you please explain why I am getting this?
import os
mypath = 'C:\\Users\\C2D\\Desktop\\Embedded system\\u1.PNG'
os.chdir(mypath)
print(os.path.getsize(mypath))
error is :
NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\Users\C2D\Desktop\Embedded system\u1.PNG'
I am working on windows 8.1 and using python3.8 on pycharm
mypath is a file and not a folder.
With the command os.chdir(mypath) you are trying to change the folder - into an image.
It is generally very important, in which exact line an exception occurs. In this case it will be line 4.
To solve your problem: You can probably just delete this line.
It is failing because of the line os.chdir(mypath). You don't need to chdir().
Assuming the path is correct and the file exists, it should work (print the file size) if you remove the os.chdir() statement.

subprocess.call throwing [WinError 123] The filename, directory name, or volume label syntax is incorrect

Im trying to execute a Rscript from my python program via subprocess.call method. However, python kept throwing the following message:
[WinError 123] The filename, directory name, or volume label syntax is incorrect
Ive checked the that both the file name and the path are legit and correct. Could someone please guide me what's wrong? Was it the syntax of the code? If so, could someone please guide me as im not familiar with subprocess syntax.
subprocess.call(['"C:/Program Files/R/R-3.4.1/bin/Rscript.exe"', '"C:/Cygwinx64/home/vettra/Python Play Zone/test2.R"'] , executable='"C:/Program Files/R/R-3.4.1/bin/Rscript.exe"')

Python Error 123 The filename, directory name, or volume label syntax is incorrect

I am trying to make directory using os.mkdir in python
It works fine when path is D:\screenshots\data
os.mkdir("D:\screenshots\data")
but it gives error when path is D:\screenshots\subs:air.com.freshplanet.games.MoviePop:moviepop.vip.1month
os.mkdir("D:\screenshots\subs:air.com.freshplanet.games.MoviePop:moviepop.vip.1month")
[Error 123] The filename, directory name, or volume label syntax is incorrect: 'D:\\screenshots\\subs:air.com.freshplanet.games.MoviePop:moviepop.vip.1month'
I don't know why it is giving this error
I experienced a similar error while working in jupyter notebook.similar to
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect:
And i figured it out to be a double slash instead of single. I don't know why i had to give double back slash(\) instead of single. How ever it solved my problem Even before a day i used the same single back slash() to change directory. Please comment if someone knows about it.

Categories