I'm trying to replace '\' with '\\', but its not working as i thought.
I tried using f.replace('\','\\'), but it is giving error like unexpected character after line continuation character.
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.dcap' in file:
files.append(os.path.join(r, file))
for f in files:
print(f)
f.replace( '\' , '\\')
os.system("dcapgen.exe f")
Basically i want to run .exe file on all the .dcap files in the given directory(path). while printing f in files, its giving file path name like C:\Users\folder\a.dcap. but while giving os.system("dcapgen.exe f"), because f is representing C:\Users\folder\a.dcap and single backslash is not recognized by python, its throwing error like
Tracing file: f
failed.
The file can still be processed, but the total frame count may show as zero and an error message may be generated for the final frame.
Cannot open file: "f" (No such file or directory)
so, i want to replace single backslash with double one. And i tried using os.system("dcapgen.exe C:\\Users\\folder\\a.dcap") and its working fine.
I would suggest to solve the problem differently. First of all there is the built-in package glob, which gives you the opportunity to use the asterisk as wildcard character to filter for the files of a certain type. Secondly you do not have to replace the file separators.
CODE
import glob
import os
files = glob.glob("{}{}*.dcap".format(path, os.filesep)
for f in files:
print(f)
os.system("dcapgen.exe \"{}\"".format(f))
EXPLANATION
The backslash character is escaping everything. So when you write '\', '\\', it actually resolves to this string ', plus the invalid sequence \\'. This is the cause for the error regarding the line continuation.
If you want to use the value of a string variable in another string you have to use a formatted string. I would recommend to use str.format instead of the modulo operation because it is more safe in terms of mishaps. In the error message it says that the file name "f" cannot be resolved by the operating system. This does not mean value of f but the literal string "f" as file name.
f is a variable. To use a variable in a string yuu'll need string Interpolation
You could use %-formatting
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.dcap' in file:
files.append(os.path.join(r, file))
for f in files:
print(f)
f.replace( '\' , '\\')
os.system("dcapgen.exe %s" % f)
Related
I need to loop some stock tickers, and then output the files using the stock ticker:
without changing the filename
without the computer thinking that it's the wrong path because of the /
what's the solution? I tried:
for ticker in (['MSFT','BF/B','AAPL']):
file_name = "{}_prices.csv".format(ticker)
with open("/Users/my_mac/download/{}".format(file_name), "w") as f:
f.write('whatever')
the above code doesn't work because python thinks BF is the directory and B_prices.csv is the filename. I use chatgpt for suggestion, but it's not smart enough to give me a solution. It told me to:
use the os.path.join function to join the components of the file path
encode the file name using urllib.parse.quote() function, which will encode the special characters in the file name, including the slash /
replace the '/' character with an underscore ('_') or any other character that is a valid filename character.
and other stupid methods that doesn't work
I can't change the filename because it's the stock ticker, it has to be the same to run other code for something else after.
the robot suggested me to try the below also, but didn't work.
file_name = "{}_prices.csv".format(ticker)
file_path = "/Users/my_mac/download/{}".format(file_name)
directory = os.path.dirname(file_path)
if not os.path.exists(directory):
os.makedirs(directory)
with open(file_path, "w") as f:
f.write(response.text)
What's the solution?
I have a folder with csv formated documents with a .arw extension. Files are named as 1.arw, 2.arw, 3.arw ... etc.
I would like to write a code that reads all the files, checks and replaces the forwardslash / with a dash -. And finally creates new files with the replaced character.
The code I wrote as follows:
for i in range(1,6):
my_file=open("/path/"+str(i)+".arw", "r+")
str=my_file.read()
if "/" not in str:
print("There is no forwardslash")
else:
str_new = str.replace("/","-")
print(str_new)
f = open("/path/new"+str(i)+".arw", "w")
f.write(str_new)
my_file.close()
But I get an error saying:
'str' object is not callable.
How can I make it work for all the files in a folder? Apparently my for loop does not work.
The actual error is that you are replacing the built-in str with your own variable with the same name, then try to use the built-in str() after that.
Simply renaming the variable fixes the immediate problem, but you really want to refactor the code to avoid reading the entire file into memory.
import logging
import os
for i in range(1,6):
seen_slash = False
input_filename = "/path/"+str(i)+".arw"
output_filename = "/path/new"+str(i)+".arw"
with open(input_filename, "r+") as input, open(output_filename, "w") as output:
for line in input:
if not seen_slash and "/" in line:
seen_slash = True
line_new = line.replace("/","-")
print(line_new.rstrip('\n')) # don't duplicate newline
output.write(line_new)
if not seen_slash:
logging.warn("{0}: No slash found".format(input_filename))
os.unlink(output_filename)
Using logging instead of print for error messages helps because you keep standard output (the print output) separate from the diagnostics (the logging output). Notice also how the diagnostic message includes the name of the file we found the problem in.
Going back and deleting the output filename when you have examined the entire input file and not found any slashes is a mild wart, but should typically be more efficient.
This is how I would do it:
for i in range(1,6):
with open((str(i)+'.arw'), 'r') as f:
data = f.readlines()
for element in data:
element.replace('/', '-')
f.close()
with open((str(i)+'.arw'), 'w') as f:
for element in data:
f.write(element)
f.close()
this is assuming from your post that you know that you have 6 files
if you don't know how many files you have you can use the OS module to find the files in the directory.
I'm walking through a directory and want to write all files names into a file. Here's the piece of code
with open("c:/Users/me/filename.txt", "a") as d:
for dir, subdirs, files in os.walk("c:/temp"):
for f in files:
fname = os.path.join(dir, f)
print fname
d.write(fname + "\n")
d.close()
The problem I have is, there are some files that are named in Chinese characters. By using print, I can see the file name correctly in console, but in the target file, it's just a mess... I've tried to open the file like open(u"c:/Users/me/filename.txt", "a"), but it did not work. I also tried to write fname.decode("utf-16"), still does not work...
In Python 2, it's a good idea to use codecs.open() if you're dealing with encodings other than ASCII. That way, you don't need to manually encode everything you write. Also, os.walk() should be passed a Unicode string if you're expecting non-ASCII characters in the filenames:
import codecs
with codecs.open("c:/Users/me/filename.txt", "a", encoding="utf-8") as d:
for dir, subdirs, files in os.walk(u"c:/temp"):
for f in files:
fname = os.path.join(dir, f)
print fname
d.write(fname + "\n")
No need to call d.close(), the with block already takes care of that.
Use str.encode() to encode fname before you write it to the file:
d.write(fname.encode('utf8') + '\n')
The key is to tell python to prepare the file for being used in "utf-8" format. I wonder why python doesn't assume utf-8 by default. Anyway, try the following:
with open("c:/Users/me/filename.txt", "a", encoding='utf-8') as d:
for dir, subdirs, files in os.walk("c:/temp"):
...
I am using python3.5. So, please be aware that the "encoding" option may be not available in python 2.7. But the idea is to tell python in advance about the encoding, rather than fighting with encoding of each string later.
To succesfully write chinese characters in python 2 you have to do the following.
Open the file using the codecs library which allows you to provide
the encoding parameter and set it to unicode.
Write the string in
unicode encoding.
The corrected code would be the following:
import codecs
with codecs.open("c:/Users/me/filename.txt", "a", encoding='utf-8') as d:
for dir, subdirs, files in os.walk("c:/temp"):
for f in files:
fname = os.path.join(dir, f)
print fname
d.write(fname.decode('utf-8') + "\n")
Note
The same problem does not exist in python 3 so you should also consider making your script python 3 compatible.
with open("xyz.xml', "w", encoding='utf-8-sig') as f:
worked for me.
I'm trying to run Python 3.3 code from a file with paths ("C:\Users\Documents\ect.") in it. When I try to run exec(commands), it returns this error:
tuple: ("(unicode error) 'unicodeescape' codec can't decode bytes in position ...
which I know is because of the single backslash character in the file paths, I know it works if it is backslashbackslash instead, but I don't know how to swap backslashbackslash for backslash. My code looks something like this:
filepath = HardDrive + "/Folder/" + UserName + "/file.txt"
file = open(filepath, 'r')
commands = file.read()
exec(commands)
The file simply has a command like this in it
os.remove("C:\Users\Documents\etc.")
The file path in the function in the file is returned automatically and I have no control over it.
Add a raw string r using str.replace to escape the filename inside the file:
with open("{}/Folder/{}/file.txt".format(HardDrive, UserName)) as f:
(exec(f.read().replace("C:\\",r"C:\\")))
Now the filename will look like 'C:\\Users\\Documents\\etc.'.
You also may need to remove that period:
exec(f.read().rstrip(".").replace("C:\\",r"C:\\"))
A simple
commands = commands.replace('\\', '/')
placed just before the exec(commands) would fix the problem if it's indeed all about the presence of backslashes (as it will turn each and every one of them into a forward slash).
Of course that's a problem if there are in the file also backslashes you want to keep as such (this simple code can't distinguish which ones you want to keep, which ones to replace!) but from your problem description this should not bother you in this case.
You can use r before your path, it will ignore all escape characters.
os.remove(r"C:\Users\Documents\etc.")
Like this.So,
file = open(r"filepath", 'r')
Beyond that, both Windows and Linux accepting / this for file paths. So you should use this. Not \, use this one /.
After your comment here;
file = open(r"{}".format(filepath), 'r')
Assume your variable is;
filepath = "c:\users\tom"
Put r before it and;
filepath = r"c:\users\tom"
Then use;
file = open(r"{}".format(filepath), 'r')
My final edit after you edited your question.
filepath = r"{}/Folder/{}/file.txt".format(HardDrive,UserName)
file = open(r"{}".format(filepath), 'r')
I am trying to open a specific file in many folders, with the name ending '.alleles.txt'. I search for the file and it is found, but then it returns:
IOError: [Errno 2] No such file or directory: 'abca3.alleles.txt'
when I try and open it.
for eachfile in filelisting:
if re.search('\.alleles\.txt$', eachfile):
allelesfile = open(eachfile, 'r')
print '2'
Directory is specified by:
folder = 'E:\\All Data'
folderlisting = os.listdir(folder)
for eachfolder in folderlisting:
print eachfolder
if os.path.isdir(folder + '\\' + eachfolder):
filelisting = os.listdir(folder + '\\' + eachfolder)
You should be using glob. Try this
import glob
files = glob.glob('E:\\All Data\\*\\*.alleles.txt')
How did you obtain the filelisting? If you used filelisting = os.walk(directoryname) then it resturns a special tuple. Did you look at it?
And, does filelisting contain the full path of the files? If the files are in a directory other than the script directory, say it's stored in the directoryname variable, you have to open them this way:
allelesfile = open(directoryname + "\\" + eachfile, 'r')
We can't give more information about your question until you put information here (actual filelisting value, etc).
I have a suspicion that some of your filenames contain non-ASCII characters. Try
folder = u'E:\\All Data'
folderlisting = os.listdir(folder)
for eachfolder in folderlisting:
print eachfolder
if os.path.isdir(os.path.join(folder,eachfolder)):
filelisting = os.listdir(os.path.join(folder,eachfolder))
Note the u'...' prefix. Without it, os.listdir() might silently drop or ASCIIfy non-ASCII characters in your filenames, which then of course leads to invalid filenames. See also this question.
This is stated in the docs for os.listdir(path) :
On Windows NT/2k/XP and Unix, if path is a Unicode object, the result
will be a list of Unicode objects. Undecodable filenames will still be
returned as string objects.
The error appears because it is trying to open the file from the directory where you are running the python program; and not the directory where the file resides.
You need to give open the full path to your file (including the directory name).