listing directories having "-" character in directories name - python-2.7

I want to list the directories in current directory having "-" character in directories name. I used os.listdir(path). Its giving me error :
"WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect:"
Any help will be greatly appreciated

Use os.listdir to get directory contents and then filter using os.path.isdir to check if each item is a dir:
dirs_with_hyphen = []
for thing in os.listdir(os.getcwd()):
if os.path.isdir(thing) and '-' in thing:
dirs_with_hyphen.append(thing)
print dirs_with_hyphen # or return, etc.
And that can be shortened using list comprehension:
dirs_with_hyphen = [thing for thing in os.listdir(os.getcwd()) if os.path.isdir(thing) and '-' in thing]
I'm using os.getcwd but you could pass in any string that represents a folder.
If you're getting an error about the filename being incorrect, you're probably not escaping correctly or it's not pointing to the right folder (absolute vs relative path issue).

I did some testing and I managed to get your error. I don't know if this is what you did to get the error though since no example has been provided.
What I did though is give an invalid drive path. Not one that could be valid and doesn't exist, one that is always wrong eg.'C::\' or 'CC:\' just anything that's not 'C:\'. As for your question.
Path should generally look like this, prefixing with r to ignore backslash as escape character or double backslash.
import os
path = r"C:\Users\Steven\Documents\"
path = "C:\\Users\\Steven\\Documents\"
for file in os.listdir(path):
if os.path.isdir(path+file) and '-' in file:
print path + file
#List Comp
[path+file for file in os.listdir(path) if os.path.isdir(path+file) and '-' in file]

Related

I am writing a DXL script to print a modulename with path using a function . I need to remove if the directory has remote in that path

Below module is forming a directory from a path
Checkmodules("Cus", "projectname".cfg", ".*(Cus|Cer Requirements|Cer Speci|ASM|/ASM24e|Specs).*");
The above line forms below folder directory, so I need to modify it when the directory has remote in the path its should be removed entire directory.
Output path
Cus/spec/cer/
cus/val_remote/value - this also needs to be removed
Cus/sys/remote ---- to be removed entire path
You can do a replace (to an empty string) with the following regex combined with the flag g (global=all occurrences) and m (multiline)
^.*remote.*$
In JavaScript this would be :
outputPath.replace(/^.*remote.*$/gm, '')

The glob.glob function to extract data from files

I am trying to run the script below. The intention of the script is to open different fasta files one after the other, and extract the geneID. The script works well if I don't use the glob.glob function. I get this message TypeError: coercing to Unicode: need string or buffer, list found
files='/home/pathtofiles/files'
#print files
#sys.exit()
for file in files:
fastas=sorted(glob.glob(files + '/*.fasta'))
#print fastas[0]
output_handle=(open(fastas, 'r+'))
genes_files=list(SeqIO.parse(output_handle, 'fasta'))
geneID=genes_files[0].id
print geneID
I am running of ideas on how to direct the script to open when file after another to give me the require information.
I see what you are trying to do, but let me first explain why your current approach is not working.
You have a path to a directory with fasta files and you want to loop over the files in that directory. But observe what happens if we do:
>>> files='/home/pathtofiles/files'
>>> for file in files:
>>> print file
/
h
o
m
e
/
p
a
t
h
t
o
f
i
l
e
s
/
f
i
l
e
s
Not the list of filenames you expected! files is a string and when you apply a for loop on a string you simply iterate over the characters in that string.
Also, as doctorlove correctly observed, in your code fastas is a list and open expects a path to a file as first argument. That's why you get the TypeError: ... need string, ... list found.
As an aside (and this is more a problem on Windows then on Linux or Mac), but it is good practice to always use raw string literals (prefix the string with an r) when working with pathnames to prevent the unwanted expansion of backslash escaped sequences like \n and \t to newline and tab.
>>> path = 'C:\Users\norah\temp'
>>> print path
C:\Users
orah emp
>>> path = r'C:\Users\norah\temp'
>>> print path
C:\Users\norah\temp
Another good practice is to use os.path.join() when combining pathnames and filenames. This prevents subtle bugs where your script works on your machine bug gives an error on the machine of your colleague who has a different operating system.
I would also recommend using the with statement when opening files. This assures that the filehandle gets properly closed when you're done with it.
As a final remark, file is a built-in function in Python and it is bad practice to use a variable with the same name as a built-in function because that can cause bugs or confusion later on.
Combing all of the above, I would rewrite your code like this:
import os
import glob
from Bio import SeqIO
path = r'/home/pathtofiles/files'
pattern = os.path.join(path, '*.fasta')
for fasta_path in sorted(glob.glob(pattern)):
print fasta_path
with open(fasta_path, 'r+') as output_handle:
genes_records = SeqIO.parse(output_handle, 'fasta')
for gene_record in genes_records:
print gene_record.id
This is way I solved the problem, and this script works.
import os,sys
import glob
from Bio import SeqIO
def extracting_information_gene_id():
#to extract geneID information and add the reference gene to each different file
files=sorted(glob.glob('/home/path_to_files/files/*.fasta'))
#print file
#sys.exit()
for file in files:
#print file
output_handle=open(file, 'r+')
ref_genes=list(SeqIO.parse(output_handle, 'fasta'))
geneID=ref_genes[0].id
#print geneID
#sys.exit()
#to extract the geneID as a reference record from the genes_files
query_genes=(SeqIO.index('/home/path_to_file/file.fa', 'fasta'))
#print query_genes[geneID].format('fasta') #check point
#sys.exit()
ref_gene=query_genes[geneID].format('fasta')
#print ref_gene #check point
#sys.exit()
output_handle.write(str(ref_gene))
output_handle.close()
query_genes.close()
extracting_information_gene_id()
print 'Reference gene sequence have been added'

How to add prefix in glob.glob for path patching

there:
I am trying to use a prefix as a input in glob.glob() function to pull out the png files in my folder. For example: I have dog_1.png, dog_2.png, bird_1.png, bird_2.png in this folder. my input is dog but for some reason, python pulled out nothing. Can you please do me a favor check where I did wrong? Thank you in advance!
dir_name = 'mypath'
if __name__=='__main__':
prefix = raw_input('Input the prefix of images:')
files = glob.glob( dir_name + prefix + '*.png')
print files
What I got is []
Your path to glob is malformed. You'll need to specify your path like this: dir_name/<file>.png (note the forward slash /). You can do this nicely using os.path.join.
import os
glob.glob(os.path.join(dir_name, prefix + '*.png'))

Running EXE files in a folder sequence

I have a sequence of folders (planx 1 , planx 2 ,.......,planx 35) each folder contains an exe file triton.exe, I wrote the following code but it gives me a syntax error,apparently the format I wrote especially '+str(i)' is wrong.
I tried to correct that by adding " "before +str(i) but it reads the folder name (planx1) without space and surly there is no such folder.
what can I do to make it work ?
import sys, string, os
for i in range(1, 35):
os.chdir( 'E:\\project\\x\\CR 0\\planx'+str(i))
os.system( '"E:\\project\\x\\CR 0\\planx'+str(i)'\\triton.exe"')
print('done')
You are missing the + after the str(i) to add the third string as well:
'...\planx'+str(i)+'\\trit...'
# ^ this one
although you may want to use os.path.join instead of adding them together

Registered Trademark: Why does strip remove ® but replace can't find it? How do I remove symbols from folder and file names?

If the registered trademark symbol does not appear at the end of a file or folder name, strip cannot be used. Why doesn't replace work?
I have some old files and folders named with a registered trademark symbol that I want to remove.
The files don't have an extension.
folder: "\data\originals\Word Finder®"
file 1: "\data\originals\Word Finder® DA"
file 2: "\data\originals\Word Finder® Thesaurus"
For the folder, os.rename(p,p.strip('®')) works. However, replace os.rename(p,p.replace('®','')) does not work on either the folder or the files.
Replace works on strings fed to it, ie:
print 'Registered® Trademark®'.replace('®',''). Is there a reason the paths don't follow this same logic?
note:
I'm using os.walk() to get the folder and file names
I have been unable to recreate your issue, so I'm not sure why it isn't working for you. Here is a workaround though: instead of using the registered character in your source code with the string methods, try being more explicit with something like this:
import os
for root, folders, files in os.walk(os.getcwd()):
for fi in files:
oldpath = os.path.join(root, fi)
newpath = os.path.join(root, fi.decode("utf-8").replace(u'\u00AE', '').encode("utf-8"))
os.rename(oldpath, newpath)
Explicitly specifying the unicode codepoint you're looking for can help eliminate the number of places your code could be going wrong. The interpreter no longer has to worry about the encoding of your source code itself.
My original question 'Registered Trademark: Why does strip remove ® but replace can't find it?' is no longer applicable. The problem isn't strip or replace, but how os.rename() deals with unicode characters. So, I added to my question.
Going off of what Cameron said, os.rename() seems like it doesn't work with unicode characters. (please correct me if this is wrong - I don't know much about this). shutil.move() ultimately gives the same result that os.rename() should have.
Despite ScottLawson's suggestion to use u'\u00AE' instead of '®', I could not get it to work.
Basically, use shutil.move(old_name,new_name) instead.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import shutil
import os
# from this answer: https://stackoverflow.com/q/1033424/3889452
def remove(value):
deletechars = '®'
for c in deletechars:
value = value.replace(c,'')
return value
for root, folders, files in os.walk(r'C:\Users\myname\da\data\originals\Word_4_0'):
for f in files:
rename = remove(f)
shutil.move(os.path.join(root,f),os.path.join(root,rename))
for folder in folders:
rename = remove(folder)
shutil.move(os.path.join(root,folder),os.path.join(root,rename))
This also works for the immediate directory (based off of this) and catches more symbols, chars, etc. that aren't included in string.printable and ® doesn't have to appear in the python code.
import shutil
import os
import string
directory_path = r'C:\Users\myname\da\data\originals\Word_4_0'
for file_name in os.listdir(directory_path):
new_file_name = ''.join(c for c in file_name if c in string.printable)
shutil.move(os.path.join(directory_path,file_name),os.path.join(directory_path,new_file_name))