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:


will be saved as


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')
    mode = 'valid'

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

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

this is output:

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
  • 1
    Where are you seeing these quotes? In the output of ls?
    – Kusalananda
    Commented Sep 14, 2018 at 10:02
  • its not my code, i can reproduce it in 4 lines of code. dimko@(none)dimko's Desktop ~/PycharmProjects/p2p_message_a7 $ python3 Python 3.6.5 (default, Apr 19 2018, 00:31:26) [GCC 6.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> filename = '[somefile]' >>> f=open(filename, 'w') >>> f.write ('something') 9 >>> f.close() >>> ls -lah gives this: -rw-r--r-- 1 dimko dimko 9 Sep 14 11:40 '[somefile]'
    – user57766
    Commented Sep 14, 2018 at 10:44
  • 2
    man ls, /--quoting-style Commented Sep 14, 2018 at 11:12

2 Answers 2


The actual filename is:


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.


The fact that the quoted filename is still aligned with the other filenames in your ls -lah output makes me think that the quotes are probably added by your ls command, and are not a real part of the filename. The purpose is probably to indicate "this filename needs quoting on the command line, or else unexpected things may happen."

Try creating a similar filename on the command line and see what happens:

touch /tmp/file\[\].txt
ls -lah /tmp

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .