I build one web and user can enter directory path in form.
My program will extract the path and write into one file.
My question is when the path include some special word such as \t \n, the program can't write file correctly.
For example:
C:\abc\test will become C:\abc[TAB]est
How can I change the string into other type like raw string and write file correctly ?
Thank you for your reply.
Use r'C:\abc\test' or 'C:\\abc\\test' when you enter your strings
... user can enter directory path in form.
...
My question is when the path include some special word such as \t \n, the program can't write file correctly.
Uh, no. Python is perfectly capable of distinguishing between '\t' and '\\t' unless you are doing something to confuse it.
>>> raw_input()
<Ctrl-V><Tab>
'\t'
>>> raw_input()
\t
'\\t'
Related
I'm trying to write to a file called
data/STO: INVE-B-TIME_SERIES_DAILY_ADJUSTED-1.csv
using the following python code on windows
overwrite = "w" # or write new
f = open(OutputPath(copyNr), overwrite)
f.write(response.content.decode('utf-8'))
f.close()
print(f"Wrote to file: {OutputPath(copyNr)}")
The output in the console is correct, but the file written to results in only data/STO so the path seems to be clipped. I've tried to escape the characters using the method from this SO answer but that gave me an invalid_argument exception for the following filename:
data/STO\\:\\ INVE-B-TIME_SERIES_DAILY_ADJUSTED-1.csv
I get the same error when I remove the space and it still seems to clip at :. How do I include such characters in a filename?
You can't. Colons are only allowed on the volumeseperator - anywhere else there are illegal.
Allowed:
d:/somedir/somefile.txt
Illegal:
./some:dir/somefile.txt
and as seperators you can either use '\\' or '/' - both should be understood.
This is not a python limitation but a operating system limitation.
See f.e. What characters are forbidden in Windows and Linux directory names?
Windows API will not allow colon in filename, it is reserved for drive letter, use a different sign.
I have an entry field that allows the user to enter their own directory/path structure when saving a file, if the user just copies their directory from windows explorer you can get text like "c:\directory\file.ext" how can I take text entered like that and replace it with the necessary "\" for the os module to use?
Use a raw string to get the backslashes
>>> path = r"c:\test\123"
>>> path
'c:\\test\\123'
>>> print path
c:\test\123
maybe os.path.normpath() would be helpful to you - and the documentation here
I want to save a dataframe to a .csv file with the name '123/123', but it will split it in to two strings if I just type like df.to_csv('123/123.csv').
Anyone knows how to keep the slash in the name of the file?
You can't use a slash in the name of the file.
But you can use Unicode character that looks like slash if your file system support it http://www.fileformat.info/info/unicode/char/2215/index.htm
... "/" is used for indicating a path to a child folder, so your filename says the file 123.csv is in a folder "123"
however, that does not make it entirely impossible, just very difficult see this question:
https://superuser.com/questions/187469/how-would-i-go-about-creating-a-filename-with-invalid-characters-such-as
and that a charmap can find a character that looks like it, which is legal. In this case a division character
You can not use any of these chars in the file name ;
/:*?\"|
You can use a similar unicode character as the "Fraction slash - Unicode hexadecimal: 0x2044"
Example:
df.to_csv("123{0}123".format(u'\u2044'.encode('utf-8')))
It gives you the filename that you asked.
When I open a file in Python and read it in I get this format: Data\r\n\r\n\t
When I paste it I get text without formatting characters, which I encode like this:
encoded = clipboard.encode('utf8')
The result looks like this: Data\n\n\t
I need the first result with the \r\n instead of just \n
Is there a different .encode to use?
Or another simple way to end up with the \r\n characters?
Thanks
The linux command unix2dos can help you out here, probably.
Reverse command is dos2unix.
Both commands are used to change formats between LF-ended and CRLF-ended line files.
encoded = clipboard.encode('utf8').replace("\n","\r\n")
I suspect clipboard is removing the "\r\n" and changing it to the more standard "\n" ... this will simply replace all "\n" with "\r\n"
I am a little stuck with zed shaw's exercise 15.
Actually I had no problem with the original program,
but the problem is when I try out the extra credit
where he asks us to use raw input instead of argv.
so, this is the code I used
filename=raw_input("enter filename :")
print "here's your file %r" % filename
txt=open(filename)
print txt.read()
when it asks for filename, I give the path e:\python\ex15_sample.txt
I am getting the following error in
this line --> txt = open(filename)
and it further says
no such file or directory
So, what do I do?
Your code is just fine. You made an error when entering the filename. Check that the file really exists.
>>> filename=raw_input('enter filename :')
enter filename :c:\Users\All Users\Autodesk\Revit\Addins\2012\RevitLookup.addin
>>> txt = open(filename)
>>> print txt.read()
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<RevitAddIns>
<AddIn Type="Application">
<Assembly>C:\Program Files (x86)\Revit 2012 SDK\RevitLookup\CS\bin\Debug\RevitLookup.dll</Assembly>
<ClientId>356CDA5A-E6C5-4c2f-A9EF-B3222116B8C8</ClientId>
<FullClassName>RevitLookup.App</FullClassName>
<Name>Revit Lookup</Name>
<VendorId>ADSK</VendorId>
<VendorDescription>Autodesk, www.autodesk.com</VendorDescription>
</AddIn>
</RevitAddIns>
(uh, that's just some file I happen to have lying around on my pc...)
Be sure to NOT use quotation marks when entering the file - or strip them of afterwards! Doing this for argv might work, but definitly not for raw_input.
EDIT: I think that's the problem: You are entering the filename with quotation marks (such as you get when you do shift-right-click "Copy As Path" in explorer). For sys.argv, these are removed by (Python? OS? I think Python...), but not with raw_input.
Due to the fact that you are using Windows you could try have to use either a / (forward slash) when entering a filename or use a double backslash for the path-seperator \\.
To enter your filename you could then try e:/python/ex15_sample.txt or e:\\python\\ex15_sample.txt.
I used the following program to finally get it work:
print "Type your filename:"
filename = raw_input(">")
txt = open(filename)
print txt.read()
I kind of puzzled why the OP used %r formatter. I didn't use it and my program still work. Is there something I'm missing? Thanks.