Python Forum
File creating with question mark quffix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File creating with question mark quffix
#1
Hi All,

Expectation is to get a file as "pre.hostname" if doesn't exist in current directory but i am getting "pre.hostname?" instead.
Can someone correct me?

I am using python-3.3.1 on RHEL 7 OS

import subprocess
from subprocess import check_output
import os
import os.path

cur_dir = os.getcwd()
#pwd = check_output("pwd", shell=True).decode()
hostname = check_output("hostname", shell=True).decode()
pre = ("/pre.")
post = ("/post.")

print ()
print ("Current Directory :", cur_dir)

fname = (cur_dir+pre+hostname)
print ("Checking the existence of File", fname)
if os.path.isfile(fname):
   print ("File Exists")
else:
   print ("File Doesn't exist; Creating a new one")
   file1 = open(fname, "w").close()
Reply
#2
On my computer, the string returned by check_ouptut("hostname", shell=True) has a newline at the end of the line. Combined with the decode this may be the issue. You can try this
hostname = check_ouptut(['hostname']).strip().decode()
Reply
#3
Your code worked. Thank you Gribouillis.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help creating shell scrip for python file marciokoko 10 1,256 Sep-16-2023, 09:46 PM
Last Post: snippsat
  Question about Creating an Automated Process in Python Supratik1234 0 712 Jan-13-2023, 08:29 PM
Last Post: Supratik1234
  Creating csv files from Excel file azizrasul 40 5,327 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  Mark outlook emails as read using Python! shane88 2 6,450 Feb-24-2022, 11:19 PM
Last Post: Pedroski55
  Doctesting a function which prints a students name along with the maximum mark scored sean1 5 2,218 Feb-01-2022, 12:20 PM
Last Post: Pedroski55
  Creating file with images BobSmoss 1 1,350 Jan-08-2022, 08:46 PM
Last Post: snippsat
  Something wrong with the quotation mark in dictionary definition Mark17 1 1,958 Jan-29-2021, 03:34 PM
Last Post: buran
  Creating csv file from another file pisike 0 1,570 Nov-24-2020, 02:02 PM
Last Post: pisike
  How to mark duplicate rows in pandas Mekala 3 2,503 Sep-17-2020, 11:32 PM
Last Post: scidam
  Creating Conda env from requirments.txt file ErnestTBass 7 15,299 Apr-23-2020, 06:57 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020