0

I want write a complex string into a file in python but the problem is my text itself have a command and double quotation and single quotation too so python can't write it and assume its a command too. for example i want write below text into file.txt:

(b'[{\n  "SourceFile": "/home/parsezan/work/hamclaasy-backend/media/3da78f6d-73a0-41d0-bca6-9b237ba13cdd/4550af6d-ac08-4d22-a80c-681bd171d295.jpg",\n  "ExifToolVersion": 10.80,\n  "FileName": "4550af6d-ac08-4d22-a80c-681bd171d295.jpg",\n  "Directory": "/home/parsezan/work/hamclaasy-backend/media/3da78f6d-73a0-41d0-bca6-9b237ba13cdd",\n  "FileSize": "112 kB",\n  "FileModifyDate": "2019:08:01 16:42:27+04:30",\n  "FileAccessDate": "2019:08:01 16:42:27+04:30",\n  "FileInodeChangeDate": "2019:08:01 16:42:27+04:30",\n  "FilePermissions": "rw-r--r--",\n  "FileType": "JPEG",\n  "FileTypeExtension": "jpg",\n  "MIMEType": "image/jpeg",\n  "JFIFVersion": 1.01,\n  "ExifByteOrder": "Big-endian (Motorola, MM)",\n  "Make": "HUAWEI",\n  "Model": "CHM-U01",\n  "Orientation": "Horizontal (normal)",\n  "XResolution": 72,\n  "YResolution": 72,\n  "ResolutionUnit": "inches",\n  "Software": "CHM-U01 V100R001C185B120",\n  "ModifyDate": "2019:07:07 11:33:18",\n  "YCbCrPositioning": "Centered",\n  "DocumentName": "",\n  "ExposureTime": "1/30",\n  "FNumber": 2.0,\n  "ExposureProgram": "Program AE",\n  "ISO": 260,\n  "ExifVersion": "0210",\n  "DateTimeOriginal": "2019:07:07 11:33:18",\n  "CreateDate": "2019:07:07 11:33:18",\n  "ComponentsConfiguration": "Y, Cb, Cr, -",\n  "ShutterSpeedValue": "1/30",\n  "ApertureValue": 2.0,\n  "BrightnessValue": 0,\n  "ExposureCompensation": 0,\n  "MeteringMode": "Average",\n  "LightSource": "Daylight",\n  "Flash": "Auto, Did not fire",\n  "FocalLength": "3.8 mm",\n  "MakerNoteUnknownText": "M[64] [e0,1] [ad,f5]",\n  "UserComment": "Hisilicon Balong",\n  "SubSecTime": 506779,\n  "SubSecTimeOriginal": 506779,\n  "SubSecTimeDigitized": 506779,\n  "FlashpixVersion": "0100",\n  "ColorSpace": "sRGB",\n  "ExifImageWidth": 4160,\n  "ExifImageHeight": 3120,\n  "InteropIndex": "R98 - DCF basic file (sRGB)",\n  "InteropVersion": "0100",\n  "SensingMethod": "One-chip color area",\n  "FileSource": "Digital Camera",\n  "SceneType": "Directly photographed",\n  "CustomRendered": "Custom",\n  "ExposureMode": "Auto",\n  "WhiteBalance": "Auto",\n  "DigitalZoomRatio": 1,\n  "FocalLengthIn35mmFormat": "27 mm",\n  "SceneCaptureType": "Standard",\n  "GainControl": "None",\n  "Contrast": "Normal",\n  "Saturation": "Normal",\n  "Sharpness": "Normal",\n  "SubjectDistanceRange": "Unknown",\n  "OffsetSchema": 60,\n  "DeviceSettingDescription": "(Binary data 4 bytes, use -b option to extract)",\n  "Compression": "JPEG (old-style)",\n  "ThumbnailOffset": 1134,\n  "ThumbnailLength": 6358,\n  "ImageWidth": 800,\n  "ImageHeight": 600,\n  "EncodingProcess": "Baseline DCT, Huffman coding",\n  "BitsPerSample": 8,\n  "ColorComponents": 3,\n  "YCbCrSubSampling": "YCbCr4:2:0 (2 2)",\n  "Aperture": 2.0,\n  "ImageSize": "800x600",\n  "Megapixels": 0.480,\n  "ScaleFactor35efl": 7.1,\n  "ShutterSpeed": "1/30",\n  "SubSecCreateDate": "2019:07:07 11:33:18.506779",\n  "SubSecDateTimeOriginal": "2019:07:07 11:33:18.506779",\n  "SubSecModifyDate": "2019:07:07 11:33:18.506779",\n  "ThumbnailImage": "(Binary data 6358 bytes, use -b option to extract)",\n  "CircleOfConfusion": "0.004 mm",\n  "FOV": "67.4 deg",\n  "FocalLength35efl": "3.8 mm (35 mm equivalent: 27.0 mm)",\n  "HyperfocalDistance": "1.70 m",\n  "LightValue": 5.5\n}]\n', None)"

i try :

    result = 'echo "{result}" >> file.txt'.format(result=str(raw_result))
    subprocess.Popen(result,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          shell=True)

then try to change it into dic:

    result = 'echo "{result}" >> /home/parsezan/work/django.txt'.format(result=str({'result': str(raw_result)}))
    subprocess.Popen(result,
                          stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT,
                          shell=True)

but no one doesn't work.any idea?thank you

2
  • 3
    You really should read docs.python.org/2.0/ref/strings.html and the part about quoting.
    – Rinzwind
    Aug 1, 2019 at 12:31
  • 1
    Probably you can process the content of file.txt to escape each character with special meaning by backslash \.
    – pa4080
    Aug 1, 2019 at 12:31

1 Answer 1

1

The quotations need to be escaped with a \ to prevent them from being interpreted by python, they also need to be escaped again so that the terminal doesn't interpret them, such as the following:

import subprocess
result = "echo 'this command has single quotes' and \\\"double \\\"quotes"
subprocess.Popen(
    f'echo "{result}" >> test.txt',
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT,
    shell=True,
)

The file would be appended with:

echo 'this command has single quotes' and "double "quotes

however the better solution would be:

with open("test.txt", "w") as out_file:
    out_file.write(result)

You must log in to answer this question.

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