I'm trying to run this specific python script to create .m3u files inside subfolders but it's not working and give me an error, any help is welcome.
The m3u file is a simple tracklist of the subfolder content with specified extensions and named after the subfolder, like this (extensions .aaa and .bbb are just examples):
Folder 1 (contains 'File 1.aaa', 'File 2.aaa', etc)
Folder 1.m3u generated inside Folder 1 with this list
File 1.aaa
File 2.aaa
Folder 2 (contains 'File 1.bbb', 'File 2.bbb', etc)
Folder 2.m3u generated inside Folder 2 with this list
File 1.bbb
File 2.bbb
Here is the script called makem3u.py (not mine, I don't know much about python):
#!/usr/bin/python
"""This script will create an m3u file with a tracklist of each .aaa or .bbb
found in a subdirectory, then name the m3u after the subdirectory. This helps
with multiple disks for emulation.
"""
import os
import os.path
EXT = ['.aaa', '.bbb']
cwd = os.getcwd()
dirs = os.listdir(cwd)
for d in dirs:
contents = os.listdir(os.path.join(cwd, d))
disks = [
os.path.join(d, f) for f in os.listdir(os.path.join(cwd, d))
if os.path.splitext(f)[-1] in EXT
]
if disks:
with open(os.path.join(cwd, '{d}.m3u'.format(d=d)), 'wb') as m3u:
m3u.writelines(['{disk}\n'.format(disk=disk) for disk in disks])
I get this error when I try to run it:
Traceback (most recent call last):
File "makem3u.py", line 16, in <module>
contents = os.listdir(os.path.join(cwd, d))
NotADirectoryError: [WinError 267] The directory name is invalid: 'path\\to\\file\\makem3u.py'
makem3u.py is inside a folder with the subfolders mentioned
Windows 10, Python 3.8.5, python is installed properly and the PATH is in enviroment variables and I can run other scripts just fine
What I'm doing wrong and how can I fix this? Is there a non-python alternative like create a .bat file to do that? Sorry for so many questions, I'm a noob in these things. Thank you in advance!
Also, is there a way to batch zip all the files in the subfolders (the generated m3u + original files) and name each zip after that subfolder? This is an extra, but would be helpful if possible
Related
I have this code:
import os
directory = "JeuDeDonnees"
for filename in os.listdir(directory):
print("File is: "+filename)
This code run and prints files name in a VSCode/Python environment.
However, when I run it in Sikuli-IDE I got this error:
[error] SyntaxError ( "no viable alternative at input 'for'", )
How can I make this for loop run or is there an alternative that can work?
Answer found ;
Basically, in my Sikuli-IDE environnement, there's layers of Python, Java, Jython... interlocking each other so the Path finding was tedious.
src_file_path = inspect.getfile(lambda: None) #The files we're in
folder = os.path.dirname(src_file_path) # The folder we're in
directory = folder + "\JeuDeDonnees" # Where we wanna go
for filename in os.listdir(directory) # Get the files
print(filename)
We're telling the Path where we are with the current file, get the folder we're in, then moving to "\JeuDeDonnees" and the files.
I am trying to open a text file from a parent directory in Python. Here is the tree (simplified):
+ Project:
- main.py
+ Source:
+ Documents:
- hello.txt
+ Modules:
- second.py
I am trying to read hello.txt from second.py.
Here is the problem line in second.py:
file = open('../Documents/hello.txt', 'r')
Returns Error: FileNotFoundError: [Errno 2] No such file or directory: '../Documents/hello.txt'
Relative paths are relative to the working directory. If you are running your code in the Projects directory, the path needs to be relative to that directory, so you need to open('./Source/Documents/hello.txt')
You can either use the absolute path:
file = open('/home/user/Project/Source/Documents/hello.txt
Change the directory names upstream of Project to what you have set up.
Or if second.py is not going to move you can use relative path:
file = open('../Documents/hello.txt')
The '..' takes you to the parent directory and you can then drop down to Documents directory from there. You can also stack the '..'. For example you can get to main.py by using:
file = open('../../main.py')
I am trying to write a python script to use the linux command wc to input the amount of lines in a file. I am iterating through a directory inputted by the user. However, whenever I get the absolute path of a file in the directory, it skips the directory it is in. So, the path isn't right and when I call wc on it, it doesn't work because it is trying to find the file in the directory above. I have 2 test text files in a directory called "testdirectory" which is located directly under "projectdirectory".
Script file:
import subprocess
import os
directory = raw_input("Enter directory name: ")
for root,dirs,files in os.walk(os.path.abspath(directory)):
for file in files:
file = os.path.abspath(file)
print(path) #Checking to see the path
subprocess.call(['wc','l',file])
This is what I get when running the program:
joe#joe-VirtualBox:~/Documents/projectdirectory$ python project.py
Enter directory name: testdirectory
/home/joe/Documents/projectdirectory/file2
wc: /home/joe/Documents/projectdirectory/file2: No such file or directory
/home/joe/Documents/projectdirectory/file1
wc: /home/joe/Documents/projectdirectory/file1: No such file or directory
I don't know why the path isn't /home/joe/Documents/projectdirectory/testdirectory/file2 since that is where the file is located.
You're using the output of os.walk wrong.
abspath is related to your program's current working directory, whereas your files are in the directory as specified by root. So you want to use
file = os.path.join(root, file)
Your issue is in the use of os.path.abspath(). All that this function does is appends the current working directory onto whatever the argument to the function is. You also need to have a - before the l option for wc. I think this fix might help you:
import os
directory = input("Enter directory name: ")
full_dir_path = os.path.abspath(directory)
for root,dirs,files in os.walk(full_dir_path):
for file in files:
full_file_path = os.path.join(root, file)
print(full_file_path) #Checking to see the path
subprocess.call(['wc','-l',full_file_path])
I am trying to pull folder names from a network drive in Python. For example, if I have a mapped drive U:, I want to go through and grab all the folders in folder "example" from the path \emily\hello\example.
This is what I have tried so far.
import os
os.chdir('U:')
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
dir = os.path.join('\emily\hello\example', dirs)
os.chdir(dir)
current = os.getcwd()
new = str(current).split("\")[4]
print(new)
this causes many errors, and I am having an issue with the syntax of a shared drive on the network vs a folder locally on my computer.
I want to see them in a list so i can read line by line and compare this list to another list.
ps. i don't want the files in the folders, just the folder names in the folder
thanks~!
error message is C:\Users\212582086\AppData\Local\Continuum\Anaconda3\python.exe "C:/Users/212582086/Desktop/Vendor sort/main"
Traceback (most recent call last):
File "C:/Users/212582086/Desktop/Vendor sort/main", line 6, in
os.chdir(dir)
FileNotFoundError: [WinError 3] The system cannot find the path specified: '\emily\hello\example\Software Archive'
Process finished with exit code 1
I have a small text (XML) file that I want a Python function to load. The location of the text file is always in a fixed relative position to the Python function code.
For example, on my local computer, the files text.xml and mycode.py could reside in:
/a/b/text.xml
/a/c/mycode.py
Later at run time, the files could reside in:
/mnt/x/b/text.xml
/mnt/x/c/mycode.py
How do I ensure I can load in the file? Do I need the absolute path? I see that I can use os.path.isfile, but that presumes I have a path.
you can do a call as follows:
import os
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
This will get you the directory of the python file you're calling from mycode.py
then accessing the xml files is as simple as:
xml_file = "{}/../text.xml".format(BASE_DIR)
fin = open(xml_file, 'r+')
If the parent directory of the two directories are always the same this should work:
import os
path_to_script = os.path.realpath(__file__)
parent_directory = os.path.dirname(path_to_script)
for root, dirs, files in os.walk(parent_directory):
for file in files:
if file == 'text.xml':
path_to_xml = os.path.join(root, file)
You can use the special variable __file__ which gives you the current file name (see http://docs.python.org/2/reference/datamodel.html).
So in your first example, you can reference text.xml this way in mycode.py:
xml_path = os.path.join(__file__, '..', '..', 'text.xml')