2

I have an API scan of a large URL file, read that URL and get the result in JSON

I get the kind of url and domain like

google.com
http://c.wer.cn/311/369_0.jpg

How to change file format name using url name ".format (url_scan, dates)"

If I use manual name and it successfully creates a file, but I want to use it to read all URL names from the URL text file used for file name

The domain name is used for json file name and created successfully without errors

dates = yesterday.strftime('%y%m%d')
savefile = Directory + "HTTP_{}_{}.json".format(url_scan,dates) 
out = subprocess.check_output("python3 {}/pa.py -K {} "
                          "--sam '{}' > {}"     
                    .format(SCRIPT_DIRECTORY, API_KEY_URL, json.dumps(payload),savefile ), shell=True).decode('UTF-8') 
result_json = json.loads(out)
with open(RES_DIRECTORY + 'HTTP-aut-20{}.csv'.format(dates), 'a') as f:
      import csv 
       writer = csv.writer(f) 
      for hits in result_json['hits']: 
             writer.writerow([url_scan, hits['_date']) 
             print('{},{},{}'.format(url_scan, hits['_date']))

Only the error displayed when the http url name is used to write the json file name So the directory is not a problem

Every / shown is interpreted by the system as a directory

[Errno 2] No such file or directory: '/Users/tes/HTTP_http://c.wer.cn/311/369_0.jpg_190709.json'
2
  • Can you explain a bit better? You want to use the information google.com and http://c.wer.cn/311/369_0.jpg? Can you put this into the Python code in a variable so that we can test it Commented Jul 12, 2019 at 2:09
  • @RobKwasowski i like to get an output file name like http://c.wer.cn/311/369_0.jpg.json google.com.json But I get an error use it to write a URL name using file name Commented Jul 12, 2019 at 2:14

1 Answer 1

1

Most, if not all, operating systems disallow the characters : and / from being used in filenames as they have special meaning in URL strings. So that's why it's giving you an error.

You could replace those characters like this, for example:

filename = 'http://c.wer.cn/311/369_0.jpg.json google.com.json'
filename = filename.replace(':', '-').replace('/', '_')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.