Python – How to avoid single quotes in file name during python saving file

filenameslspython

If Python saves file on extended 4(Linux) partition that contains unusual symbol(so far uncovered square brackets [] ). It automatically quotes file name.

For example:

file[].txt

will be saved as

'file[].txt'

In Linux, extended 4 partition "file[].txt" is a valid file name.

Why is it happening and can it be avoided?
I can create a work around, by removing first and last symbol of file name, but I plan to run my app on many platforms. So I'd rather nip it in the bud early.

Python3 is used on AMD64 platform Linux Gentoo with ext4 partition.

python code:

    print('\ndebug:triggered sf mode, name of file below \n')
    clientsocket, addr = serversocket.accept()
    filename = clientsocket.recv(10240)
    filename = filename.decode('UTF-8')
    app_output(filename)
    mode = 'valid'
    clientsocket.close()

    clientsocket, addr = serversocket.accept()
    incmdata = '1'
    somefile = open(filename, 'w+')
    somefile.close()
    while len(incmdata) !=0:
        incmdata = clientsocket.recv(1024 * 8)
        print('this is filename: ', filename)
        somefile=open(filename, 'ab')
        somefile.write(incmdata)
        somefile.close()
    app_output('file supposedly recieved')

filename variable is something client sends to miniserver.(actually client catches positional variable)

this is output:

file[].txt
this is filename:  file[].txt
this is filename:  file[].txt

also, relevant part of

ls -lah

-rw-r--r--  1 dimko dimko        269 Sep 11 10:00  file_open.txt
-rw-r--r--  1 dimko dimko         93 Sep 11 09:05  file_open.txt~
-rw-r--r--  1 dimko dimko          5 Sep 14 11:24 'file[].txt'
drwxr-xr-x  3 dimko dimko       4096 Sep 14 11:21  .idea
-rw-r--r--  1 dimko dimko       7635 Sep 14 11:20  LanSwissKnife.py

Best Answer

The actual filename is:

file[].txt

Your python program is not at any fault here. The quotes are done by ls.

I was able reproduce it using:

ls -lah --quoting-style=shell

but when I choose literal as a quoting style:

ls -lah --quoting-style=literal

then the files with [ or ] are listed without quotes. Note that you ls might have different default arguments on different systems.

Related Question